2amigos / yiiwheels

Widget extension library for the YiiStrap extension
http://yiiwheels.2amigos.us
Other
133 stars 77 forks source link

Submitting form with WhSelect2 #44

Closed re1naldo closed 11 years ago

re1naldo commented 11 years ago

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:

$this->widget('yiiwheels.widgets.select2.WhSelect2', array(
   'asDropDownList'=>false,
   'model'=>$model,
   'attribute'=>'receiver_id',
   'pluginOptions'=>array(
       'ajax'=>array(
          'url'=>$this->createUrl('user/autocomplete'),
          'dataType'=>'json',
          'data'=>'js: function (term, page) {
              return {
                 name: term,
                 self: true 
              };
          }',
          'results'=>'js: function (data, page) { 
              return {results: data};
          }'
       ),
       'formatResult'=>'js: function (data) {
          return data.name;
       }',
       'formatSelection'=>'js: function (data) {
          return data.name;
       }',
   )
));

How can we display the submitted value in WhSelect2 dropdown? I have tried to set initSelection function without any luck.

tonydspaniard commented 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

re1naldo commented 11 years ago

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.

tonydspaniard commented 11 years ago

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
    )
 );?>
re1naldo commented 11 years ago

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.

tonydspaniard commented 11 years ago

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.

re1naldo commented 11 years ago

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!