It is necessary that, depending on the selected item in the dropDownList, the output of the next field is displayed - if string, then the text field is output, if image is fileInput

<?= $form->field($model, 'type')->dropDownList([ 'string' => 'string', 'image' => 'image', ]); 

Tried to do it, but neither works

 if($model->type = 'image') { echo $form->field($model, 'file')->fileInput(); } else { echo $form->field($model, 'value')->textInput(); } 

Do I need to somehow read the active element and write to if ()?

    1 answer 1

    You need to hang the js handler on your <select> and, depending on the type chosen, show / unlock or hide / block the required fields.

    If you have only 2 fields then you can arrange to switch types using input:radio :

     $this->registerJs(<<<JS $('#type_switcher input:radio').on('click', function() { $('#type_switcher').find('input:not(:radio)').each(function(){ this.disabled = true; }); $(this).closest('.form-group').find('input:not(:radio)').each(function(){ this.disabled = false; }); }); JS ); 

    Display fields for the form

     <div id="type_switcher" class="form-group" style="margin-left: 0; margin-right: 0;"> <?= $form->field($model, 'file') ->fileInput([ 'class' => 'form-control', 'disabled' => $model->type != $model::TYPE_FILE ]) ->label( Html::activeRadio($model, 'type', [ 'value' => $model::TYPE_FILE, 'label' => $model->getAttributeLabel('file'), 'id' => Html::getInputId($model, 'type') . '_' . $model::TYPE_FILE, ]) ) ?> <?= $form->field($model, 'value') ->textInput([ 'maxlength' => true, 'disabled' => $model->type != $model::TYPE_VALUE ]) ->label( Html::activeRadio($model, 'type', [ 'value' => $model::TYPE_VALUE, 'label' => $model->getAttributeLabel('value'), 'id' => Html::getInputId($model, 'type') . '_' . $model::TYPE_VALUE, 'uncheck' => null, // !!! ]) ) ?> </div>