CActiveForm dropDownlist

Hi All

i have a database table with the following fields

PostID (auto increment)

PostType (dropdownlist with the options Cars,Mobiles,Laptops)

model (textfield)

registration city (textfield)

warranty (textfield)

condition (textfield)

i have generated the CRUD for this model and now on my partial view ( _form.php file ) i want that when the user selects Cars from PostType dropdownlist warranty field should hide because cars don’t have warranty.when the user selects Mobiles from Drop down list registration city field should hide.as same for the laptops.

How can i acheive this using Jquery and controller action

i have alot of experience with asp.net and i can do this in asp.net but not in Yii.

sample code will be highly appriciated

thank and one more thing this is my second post and i am really disappointed that my first post does not even have a single reply.im new to yii framework and need some help from the pros

thank you very much in advance

Hi, and welcome to the forum.

The following sample code shows how to use jQuery to show/hide a certain item in a form according to the selected value of a dropDownList.




<div class="form">

<?php

$form = $this->beginWidget('CActiveForm',

	array(

		'id'=>'my-model-form',

		'enableAjaxValidation' => true,

	)

);

?>


<div class="row">

<?php echo $form->labelEx($model,'type'); ?>

<?php

$selections = array(

	'A' => '1',

	'B' => '2',

);

?>

<?php echo $form->dropDownList($model, 'type', $selections); ?>

<?php echo $form->error($model,'type'); ?>

</div>


<div class="row" id="my-model-name">

<?php echo $form->labelEx($model,'name'); ?>

<?php echo $form->textField($model, 'name'); ?>

<?php echo $form->error($model,'name'); ?>

</div>


...


<?php $this->endWidget(); ?>


</div><!-- form -->


<?php

Yii::app()->clientScript->registerScript('my-model-form-functions', "

$('#MyModel_type').on('change', function(event){

	showHideNameInput();

	event.preventDefault();

}

function showHideNameInput() {

	if ($('#MyModel_type').val() == '1') {

		$('#my-model-name').show();

	} else {

		$('#my-model-name').hide();

	}

}

showHideNameInput();

");



I hope you’ll get some hint from it. :)

[color="#2E8B57"]/* note : your duplicated post in Extensions sub forum are deleted */[/color]

Thanks alot for the reply and the sample code .It solved by problem

thanks :)

[Edited]

Sorry The code is not working not even the alert box does not get displayed.initially i thought that it will work but today i tested it and it does not. please help




<div class="form">


<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'userad-form',

	'enableAjaxValidation'=>false,

)); ?>


<div class="row">

		<?php echo $form->labelEx($model,'AdType'); ?>

		<?php echo $form->dropDownList($model,'AdType',$model->getAdTypes(),

										array('prompt'=>'---Select Ad Type---')); ?>

		<?php echo $form->error($model,'AdType'); ?>

	</div>

...

...

...

<?php $this->endWidget(); ?>


</div><!-- form -->


<?php

Yii::app()->clientScript->registerScript('userad-form-functions', "

$('#Userad_AdType').on('change', function(event){

        showHideNameInput();

        event.preventDefault();

}

function showHideNameInput() {

       alert($('#Userad_AdType').val());

}

");

?>