Closed re1naldo closed 11 years ago
Was the value submitted and saved? Did you make sure the attribute is on rules? sometimes when you submit an attribute that is not even marked as safe
is not set by the setAttributes
method of the model
The value was submitted and saved successfully. The only problem is that it was not displayed in WhSelect2 dropdown when there are validation errors on other fields.
Yep... the reason is that you use autocomplete and you need to create the initSelection
to select the right value. It does not do it automatically, it is the way the plugin works.
Here is an example of what i do:
if (!$model->isNewRecord) {
$pluginOptions['initSelection'] = 'js:function(element, callback){
callback({
id:' . $model->managing_employee_id . ',
text:"' . CHtml::value($model, 'managingEmployee.fullName') . '"});
}';
}
?>
<?php
$this->widget(
'buzz.widgets.BSelect2',
array(
'model' => $model,
'attribute' => 'managing_employee_id',
'asDropDownList' => false,
'pluginOptions' => $pluginOptions
)
);?>
Actually I have tried to add initSelection
(to the code above) before posting this issue. However, even if I set the values directly:
'initSelection'=>'js:function(element, callback) {
callback({id:1, text:"xxx"});
}',
WhSelect2 dropdown is still empty. The actual value was submitted, but it is not displayed in dropdown.
Could you please tell me if there is anything else that needs to be checked? I didn't see any Javascript errors in that page.
I would get back to basics and remove the formatResult and formatSelection
and try with the initSelection
only. As I cannot find any other reason.
Sorry for my late reply. After debugging the code, I found that the initSelection
function was not called. We need to provide initial value in order to use it.
The working code is:
$pluginOptions = array(
... (same as above but without initSelection)
);
if (isset($model->receiver_id))
{
$pluginOptions['initSelection'] = 'js:function(element, callback) {
callback({name:"' .$model->receiver_id. '"});
}';
}
$this->widget('yiiwheels.widgets.select2.WhSelect2', array(
'asDropDownList'=>false,
'model'=>$model,
'attribute'=>'receiver_id',
'pluginOptions'=>$pluginOptions
));
Anyway, thanks for your time, Antonio!
I use WhSelect2 with ajax in a form and everything works fine except when I submitted the form. The real value of field was submitted but I didn't see the value in WhSelect2 dropdown.
Code:
How can we display the submitted value in WhSelect2 dropdown? I have tried to set initSelection function without any luck.