A.Miguel, on 13 April 2012 - 07:20 PM, said:
Hi,
This is in fact a Bootstrap related question; please read carefully my topic.
The main point behind my question is that I cannot use any bootstrap method to build up a dropdownlist that is not directly related with the model i.e. a CHtml object. If you notice
dropDownListRow is a bootstrap method that will execute the following:
protected function dropDownList()
{
echo $this->getLabel();
echo '<div class="controls">';
echo $this->form->dropDownList($this->model, $this->attribute, $this->data, $this->htmlOptions);
echo $this->getError().$this->getHint();
echo '</div>';
}
This requires a model instance whereas CHtml not. In my example just the city field is directly related with the Employee model whereas State is not.
I've tried to enhance Bootstrap in order to include CHtml approach (see code below) but ended with no success because $model is required although the extension:
protected function dropDownListSimple()
{
echo $this->getLabel();
echo '<div class="controls">';
echo CHtml::dropDownList($this->attribute,'', $this->data, $this->htmlOptions);
echo $this->getError().$this->getHint();
echo '</div>';
}
Feedback is very welcome.
A.Miguel
I'm afraid you would have exactly the same problem without Bootstrap as far as I can tell. Instead of using the standard Bootstrap $form->dropDownListRow function (or the Bootstrap less $form->dropDownList), you just need to use the CHtml::dropDownList($this->attribute,'', $this->data, $this->htmlOptions); way directly *in* your page.
You don't really need to use the Bootstrap extension functions, you can just build the structure yourself like you do in this code:
echo '<div class="control-group">';
echo '<label for="State" class="control-label required">State <span class="required">*</span></label>';
echo '<div class="controls">';
echo CHtml::dropDownList('StateList','', State::getStates(),array(
'ajax' => array(
//'type'=>'POST', //request type default GET
'dataType'=>'json',
'url'=>CController::createUrl('City/getCitiesByStateId'), //url to call.
'update'=>'#Employee_city_id', //selector to update
//'data'=>'js:javascript statement'
'data'=>array('stateId'=>'js:this.value'),
//leave out the data key to pass all form values through
//http://www.yiiframework.com/wiki/24/
),
'empty' => '---'
));
echo '</div>';
You might have a problem with validation this way though (there will be none on the State field). If you need that, you will have to add a "State" property to your model (just put: public $state = ''; in your model), which you can then use in your validation rules etc.
Actually that would also allow you to use the Bootstrap extension and normal form functions (as this will make "State" part of your model as if it would be in your table), so this will probably be the easiest (and best) solution for you. Your Employee model will then actually have a "State" property.
Hopefully this will help you a bit.
Cheers