piebuo
piebuo

Reputation: 151

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?

Upvotes: 7

Views: 11706

Answers (2)

Akshay
Akshay

Reputation: 209

This works perfectly.

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

Upvotes: 8

ScaisEdge
ScaisEdge

Reputation: 133360

try adding options with class eg: :

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

Upvotes: 0

Related Questions