How to know the active dropDownList in yii2



  • It is necessary that, depending on the element chosen in dropDownList, the next field - if string, we remove the text field if the image is fileInput That's the leaf code.

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

    Trying to do that, but it doesn't work.

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

    Do you have to count the active element and record it?



  • You need to put js on your processor. <select> and, depending on the type chosen, show/seal or hide the necessary fields.

    If you only have two fields, then you can arrange to switch types with help. 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
    );
    

    Let's get out of shape fields.

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


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2