Search code examples
javascriptphpcssdrop-down-menuyii2

Yii2 add class or function to dropdownlist form field


I need to add a class and a function to a dropdownlist in Yii2 activeform, here's the code:

<?php $form = ActiveForm::begin(); ?>

<?= $form->field($model, 'tipocontratto')->dropDownList(['RES' => 'Residenziale', 'BUS' => 'Business'], ['prompt'=>'Seleziona...'],['maxlenght'=> true]); ?>

I need to specify a class for the field and also a JavaScript function.

In normal textfield I use this way:

<?= $form->field($model, 'cogn_ragsoc')->textInput(['maxlength' => true,'class'=>'form-control formtesto','onfocus'=>'test()']) ?>

and it works perfectly, but in dropdownlist it doesn't.

How can I fix this?


Solution

  • This works perfectly.

    <?= $form->field($model, 'tipocontratto')->dropDownList(['RES' => 'Residenziale', 'BUS' => 'Business'], ['prompt'=>'Seleziona...','class'=>'yourclass','onchange'=>'function()']); ?>