0

I have a dropDownList in my form like this :

<?= $form->field($model, 'object_typeID')->dropDownList( ArrayHelper::map(ObjectTypes::find()->all(),'object_typeID','title'), [ 'prompt' => 'Choose Object type', ] ) ?> 

and I want to load other form elements based on selected dropDownList value before submit the form so I should have selected value immediately when user select it. How to get dropDownList selected value before submit?

3 Answers 3

0

Set the $model->object_typeID to the any value of the dropdown values. Here is an example:

$model = Model::findOne(3); echo $model->object_typeID; // let's say it prints 3 $dropdown = [1 => 'Ejaz', 2 => 'Nizar', 3 => 'Saleem']; $form->field($model, 'object_typeID')->dropDownList( $dropdown, [ 'prompt' => 'Choose Object type', ] ) // the above code will select Saleem by default. 
Sign up to request clarification or add additional context in comments.

1 Comment

My dropDownList populated with another db table! I need an Ajax that load other form elements based on user selected value of dropDownList!
0

do you mean like this? select value from dropDownList and assign value to another fields? https://www.youtube.com/watch?v=hZ-6huMYxBc&index=24&list=PLRd0zhQj3CBmusDbBzFgg3H20VxLx2mkF

1 Comment

I don't want assign value to another fields! Actually I need to print some other form elements based on user selected value probably using Ajax
0

You need to use jquery for this.

Step 1: Add an id(say 'listID') to your current field Step 2: Add a class(say 'customClassName hide') to next field Step 3: Find out the value of the selected item from the list using jquery and add/remove class as below : $('#listID').change(function () { var formValue = $(this).val(); var RequiredValue = '0'; // field to be shown on this value if(formValue == RequiredValue){ newClass = 'show customClassName'; }else{ newClass = 'hide customClassName'; } $('.customClassName').removeClass().addClass(newClass); }); 

2 Comments

Thanks but I have so many form elements from another dp table that should be conditionally (based on user select) printed in the form and I can't use show/hide class!! Any other solution?
My main issue is step 3 : Find out the value of the selected value :D

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.