Tbmodal: Simple Doubt

Hello everyone!

Knowing that cities belong to states (they have an idState), I want to create a city from state’s view.php with this code:




<?php $this->beginWidget('bootstrap.widgets.TbModal', array('id'=>'myModal')); ?>

<div class="modal-header">

    <a class="close" data-dismiss="modal">&times;</a>

    <h4>New city</h4>

</div>

<div class="modal-body">

    <?php

        $city = new City();

        $city->idState = $model->id;

        $this->renderPartial('/city/_form', array('model'=>$city), true);

    ?>

</div>

<div class="modal-footer">

    <?php $this->widget('bootstrap.widgets.TbButton', array(

        'label'=>'Close',

        'url'=>'#',

        'htmlOptions'=>array('data-dismiss'=>'modal'),

    )); ?>

</div>

<?php $this->endWidget(); ?>

<?php $this->widget('bootstrap.widgets.TbButton', array(

    'label'=>'New City',

    'type'=>'primary',

    'htmlOptions'=>array(

        'data-toggle'=>'modal',

        'data-target'=>'#myModal',

    ),

)); ?>



My problem is in:


$this->renderPartial('/localidad/_form', array('model'=>$localidad), true);

because if I set the third parameter to true it doesn’t render the form, but if I set it to false it doesn’t call the controller (never enters to actionCreate in CityController).

Anything will help. Thanks in advance.

I guess you want to render a list of cities belonging to a state ?

You are not doing that with your code…

All you do is load an empty City object and then render its fields (presumably) in renderPartial.

The 3rd parameter of renderPartial is there to give you the choice: send the output to the browser (php echo) or return the output as a string so that you can further manipulate it.

You probably want your City model to have a function like listCities($StateId) that returns an array of cities. This output then can be fed into CHtml::listData() to generate the data for an html dropdown.

hth,

Tom

Thanks but what I want is to CREATE a new city that belongs to a State. Let’s pretend we are in a particular state index, now I want to create a new city and render that _form inside a modal.

I pass


 $city->idState = $model->id;

because it BELONGS to the current state.

Ah yes, ok, sorry.

Well, you definitely do not want the 3rd parameter of renderPartial to be true, otherwise you are not rendering anything.

Probably then a problem with the post action or submit button… ? I have no experience using a TbModal to submit data. Does it create an html form element with an action attribute ? Also, you did not add ‘buttonType’=>‘submit’ to the TbButton definition.

Thanks Tom. I’ve made a mistake: I don’t need the modal footer because City’s _form already have a Submit button.

Anyway, when I press the Submit button it doesn’t do anything, just closes the modal. How can I call the CityController’s actionCreate?

:blink:

EDIT: I’ve used the code of this video and it works fine, but I have a problem. If save() in actionCreate returns false, the modal gets closed. How can I reopen the modal from the controller or avoid the modal from close?