Hello, I need to change the second parameter depending on the first one. There are 5 input types - text, image, textarea, ckeditor, checkbox

And depending on the selected input type, display the required field - text, download field, etc.

<?= $list =  $form->field($model, 'type')->dropDownList([                     'input' => 'Input',                     'image' => 'Image',                     'textarea' => 'TextArea',                     'fck' => 'FCK Editor',                     'checkbox' => 'CheckBox',                 ],                     ['id' => 'js-type']);                 ?> 

Select the desired type parameter

Script, for show, hide

 <?php                //script JS                $script = <<< JS   $('#js-type').on('change', function() {  var selection = $(this).val();  $('div#values > div').hide();  $("#"+selection).show(); }); JS;                $this->registerJs($script, View::POS_END);                ?> 

And the fields themselves

 <div id="values">                    <div id="input" style="display:none;">                        <?= $form->field($model, 'value')->textInput(['maxlength' => true]); ?>                    </div>                    <div id="image" style="display:none;">                        <?= $form->field($model, 'value')->fileInput(); ?>                    </div>                    <div id="textarea" style="display:none;">                        <?= $form->field($model, 'value')->textarea(['rows' => '6', 'maxlength' => true]) ?>                    </div>                    <div id="fck" style="display:none;">                        <?= $form->field($model, 'value')->widget(CKEditor::className(), [                            'options' => ['rows' => 3],                            'preset' => 'full'                        ]) ?>                    </div>                    <div id="checkbox" style="display:none;">                        <?= $form->field($model, 'value')->checkbox() ; ?>                    </div>                </div> 

Does not send data, i.e. If I choose the type of input - a text field, I fill it with data. But when sending an error - You must fill in the "Value". Apparently due to the fact that there are still fields

 $form->field($model, 'value'), 

because if you leave only 1 field, everything works. How can you get out of the situation, because in this way will not work? How to change only the values ​​after $ form-> field ($ model, 'value') to -> textInput , -> textarea , etc?

    1 answer 1

    I didn’t get to the point, and perhaps it’s better to go the other way, but if the very essence of the question is “how to call a method from a string”, then something like this:

     $methods = [ 0 => 'input', 1 => 'image', 2 => 'textarea', 3 => 'fck', 4 => 'checkbox' ]; $type_id = 0; $form->{$methods[$type_id]}($model, 'value');