When you use the relation name as the dropDownAttribute, that is not enough to make the dropDownList know which elements should be preselected. That’s because dropDownList needs an array of elements/values, not an array of objects.
If ‘categories’ is a relation in the model Post for example, then:
$model = Post::model()->findByPk($id);
$pcs = $model->categories; // This is an array of objects. Not a array of values.
returns an array of objects, which cannot be utilized by dropDownList.
After that, ‘$model->categories’ is an array of ids/values, that dropDownList can utilize, and the values will be preselected.
Note that after clicking ‘update’ in the form, the newly selected values will not be automatically saved in the relation table PostCategories. You have to take care of it in your actionUpdate() method of your Post model.
I recommend this article: Handling Related Models in Yii Forms from Larry Ullman.
This was actually my next question - what if I want to do it simpler (without relations)? I currently have in my model:
public static $TypeArray
= array(
1=>'TYPE1',
2=>'TYPE2',
3=>'TYPE3',
4=>'TYPE4',
5=>'TYPE5',);
and my view file has
$types = Model::$TypeArray;
$this->widget('ext.widgets.EchMultiselect', array(
'model'=>$TypeModel,
'dropDownAttribute'=>'type', //I did as you said and created new array $type in controller
'data'=>$types,
And my table has field_1, field_2, field_3, field_4, field_5, each which gets set to 0 or 1 depending on whether they get checked (working thanks to your code suggestion!).
So how do I get it to now show the already checked values after POST? Thanks so much.
This way, you fill the new variable/array type of your model according to the values in your table, before your render the form.
Since the widget in your view file has type as its dropDownAttribute, and type is an array with values, the widget will/can use these values to preselect the options.
This makes sense - I am running into an error though with $TypeModel->field_$i - my editor is saying that doesn’t work (the field_$i). I tried it with field_.$i but that doesn’t work either. Any thoughts?
// To get the value of the model-attribute
$fieldname = 'field_'.$i;
$type[$i] = $TypeModel->attributes[$fieldname];
// And if you want to set the value of the model-attribute at some point:
$TypeModel->setAttribute($fieldname, $tpye[$i]);
Great, this worked perfect. I ended up using something like this:
for($i=1;$i<=5; $i++) {
$fieldName = 'field_'.$i;
if ($TypeModel->$fieldName == 1) //check model for 1 or 0
$TypeModel->type[$i] = $i; //using $i instead of 1 since we need checkbox value, but model only stores 0 or 1
else $TypeModel->type[$i] = 0;
Again, really appreciate your help and instruction!
I tested it but I can’t get the options take the whole width of the widget. They are left-aligned (about on the 100 first px I think) even if I increase the width of the widget.
I assume that the same problem exists when you define the width via ‘width:450px’ instead of span-10?
It seems to me, that there are other style-declarations (like width, or float:right etc.) that affect the checkboxes and make them appear that way…
Do you use Firebug (the add-on for firefox)?
With firebug you could determine what styles exactly are applied to the checkboxes.
Sometime css-styles do behave unexpectedly and if there are more than one css-file, something overrides the other and there is no way to know what happpens without some tool like firebug…
It is really difficult to say something without seeing the code, but I have the suspicion that somewhere in your css files there is a style declaration for the ‘label’ element that sets a fixed width for it and makes the text within right aligend, i.e. something like this:
label {
width:100px;
text-align:right;
}
This is often done to style form elements, where a label is followed by a text-field for example, to produce forms like here: http://jeffhowden.com/code/css/forms/
Note that the labels are right aligned and how they are styled further below:
I want to display it like a multiple checkbox list but without the user to click on the scroller => always display the entire list (and abusing : is it possible in that case that the checkboxes take the entire height before the next component? - css? my weakness)