Disable Validation Form Disable Field

hello all!

i have this form with 2 droplist/

i have JS code that disable the second filed if i chose the first option.

but when i submit the form he stiil check validation to the disable filed :(

i try to find a solution on web and it has not succees

my form and JS:




 <!-- forme -->


<div class="row">

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

        <?php echo $form->DropDownList($model,'workerToRole',Role::model()->getRolesList(),array('id'=>'wrole')); ?>

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

        </div>


        <div class="row">

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

        <?php echo $form->DropDownList($model, 'workerToProfessions',  Profession::model()->getProfessionList(), array('id'=>'wpro','multiple'=>'multiple', 'size'=>5, )); ?>

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

        </div>


<!-- JS-->


<script type="text/javascript">

$(document).ready(function() {

$("#wrole").change(function() {

 if($(this).val() === "1") {

                      $("#wpro").removeAttr("disabled", "disabled");

                       

                } else {

                      $("#wpro").attr("disabled", "disabled");

                     

                }

    });

 });

 </script>




controller:





 public function actionCreate()

	{

		$model=new Worker;

		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);

		if(isset($_POST['Worker']))

		{

                     $model->attributes=$_POST['Worker'];

                     if($model->save())

                         {

                            foreach ($_POST['Worker']['workerToRole'] as $role_code) 

                                 {

                                $modelr=new workerToRole;

                                $modelr->worker_code = $model->worker_code;

                                $modelr->role_code = $role_code;

                                if (!$modelr->save()) print_r($modelr->errors);

                             }role_code ==1){// this not work!!!! <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/sad.gif' class='bbc_emoticon' alt=':(' />

                             foreach ($_POST['Worker']['workerToProfessions'] as $profession_code) 

                                 {

                                $modelp=new workerToProfession;

                                $modelp->worker_code = $model->worker_code;

                                $modelp->profession_code = $profession_code;

                                if (!$modelp->save()) print_r($modelp->errors);

                             }

                             }

                             

                             $this->redirect(array('view','id'=>$model->worker_code));

                            }

                        }

		$this->render('create',array(

			'model'=>$model,

                ));

        }




can u help me?

You don’t need JS to do this you can just create a custom validation rule in your model. you might have to adjust it some. if you have wokertoprofession required make it not required.




public function workerProfession($attributes,$params)

	{

		$worker_to_role = $_POST['Worker']['workerToRole'];

		$worker_to_professions = $_POST['Worker']['workerToProfessions'];

		if(isset($worker_to_role) && !isset($worker_to_professions)){

     	$this->addError('workerToProfessions','You must select a Profession.');

  	}

	}

	public function rules()

	{

		return array(

			array('workerToProfessions','workerProfession'),

		);

other rules......

	}



thenks!

didn’t know i cold do that…

i’ll try it.

mabe u know how to make yii not get the profssion when i dont need it?

it work!

yey!

Glad it worked. You can even take it one step futher and only show the second dropdown list if the first one is filled out. That you will need js for.




<div class="row">

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

<?php echo $form->DropDownList($model,'workerToRole',Role::model()->getRolesList(),array('id'=>'wrole', 'onchange' => 'javascript:$("#profession_list").toggle()'));

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

</div>


<div class="row" id="profession_list" style="display: none">

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

<?php echo $form->DropDownList($model, 'workerToProfessions',  Profession::model()->getProfessionList(), array('id'=>'wpro','multiple'=>'multiple', 'size'=>5, )); ?>

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

</div>