phpyii2widgetsmarty

Use form field with select2 widget in smarty yii


I would like to use this example from Kartik with Smarty.

use kartik\widgets\Select2

// Usage with ActiveForm and model
echo $form->field($model, 'state_1')->widget(Select2::classname(), [
    'data' => array_merge(["" => ""], $data),
    'options' => ['placeholder' => 'Select a state ...'],
    'pluginOptions' => [
        'allowClear' => true
     ],
]);

In Smarty only the field can be done with:

{$form->field($model, 'name')}

and only the widget:

{Select2
   name = 'my_selection'
   attribute = 'name'
   data = $data
   options = [
       'multiple' => true
   ]
}

I can't find how to combine these.


Solution

  • It should work the same way. I have played with smarty a little and for me this works just fine

    {$form->field($model, 'name')->textInput(['maxlength' => 255])}
    

    For you it should be

    {$form->field($model, 'state_1')->widget(Select2::classname(), [
        'data' => array_merge(["" => ""], $data),
        'options' => ['placeholder' => 'Select a state ...'],
        'pluginOptions' => [
            'allowClear' => true
         ],
    ])}
    

    If it does not work try not making multiple rows and put everything in 1 row.

    This is how I have done it here: https://github.com/Mihai-P/yii2-cms/blob/master/views/tag/_form.tpl