yii - Required Rule on specific condition -


i need apply rule on form. have country , dependent drop down of state.

i need apply required rule on state field.

but if choose india drop down required rule should remove form.

i have enabled clientsidevalidation true in cactiveform.

view form:

  <?php $form = $this->beginwidget('cactiveform', array(                     'id' => 'cart',                     'enableajaxvalidation' => false,                     'enableclientvalidation'=>true,                     'clientoptions'=>array('validateonsubmit'=>true),                     // need next 1 transmission of files in form.                     'htmloptions' => array('enctype' => 'multipart/form-data'),                  ));              echo $form->dropdownlist($modeluser, 'country', countries::getcountrylistwithcode(),         array('options' => array($currentcountry=>array('selected'=>true)),         'empty'=>'select country',         'class'=>'form-control input-lg',         ));         echo $form->error($modeluser,'country');           echo $form->labelex($modeluser,'state',array('class'=>"col-md-30"));            echo $form->dropdownlist($modeluser, 'state', $statelist,array('class'=>"form-control input-lg",'prompt'=>'select state'));          echo $form->error($modeluser,'state');       $this->endwidget();       ?> 

and model rules this:

public function rules() {     // note: should define rules attributes     // receive user inputs.     return array(         array('city,country,state,address_line_one,postcode', 'required'), 

you can achieve adding custom rule model.

change rules function this

public function rules() {     // note: should define rules attributes     // receive user inputs.     return array(         array('city,country,address_line_one,postcode', 'required'),         array('state', 'validatestate'),     ) } 

next create custom validation rule. this

public function validatestate ($attribute, $params) {     $acountrieswithstate = array('usa');     if (in_array($this->$attribute, $acountrieswithstate) && empty($this->$attribute))     {        $this->adderror($attribute, 'state missing');      } } 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -