Ajax activeform example

Hi,

I’m totally lost, I try to understand how to do a ‘basic’ thing with YII2 by using the best ‘YII 2 spirit’.

I have a simple form (activeform) with 2 dropdownlist inside :




<?php $form = ActiveForm::begin(['layout' => 'horizontal',

 'fieldConfig' => [

        'template' => "{label}\n{beginWrapper}\n{input}\n{hint}\n{error}\n{endWrapper}",

        'horizontalCssClasses' => [

            'label' => 'col-sm-1',

            'offset' => 'col-sm-offset-2',

            'wrapper' => 'col-sm-2',

            'error' => '',

            'hint' => '',

        ],

    ],

]); ?>

    <?= $form->field($model, 'brands')->DropDownList($BrandList); ?>

    <?= $form->field($model, 'models')->DropDownList($ModelList); ?>

    <div class="form-group">

        <?= Html::submitButton('Rechercher', ['class' => 'btn btn-primary']) ?>

    </div>

<?php ActiveForm::end(); ?> 



$BrandList and $ModelList are populated by using MySql requests.

What I’d like to do :

When the user selects a brand in the first dropdownlist, I would like to populate the second one (model list) by Ajax…

If someone can help me …

I reply myself.

One way is to use javascript event to validate the form :




<?= $form->field($model, 'brands')->DropDownList($BrandList,['onchange'=>'this.form.submit()']); ?>