scenario for validation rules in yii

前端 未结 1 848
不思量自难忘°
不思量自难忘° 2021-01-06 04:10

I was wandering is there any chance to use scenario for rules,

in my model I have

public function rules()
{
    return array(
        array(\'deliv         


        
1条回答
  •  礼貌的吻别
    2021-01-06 04:38

    Yes its possible. In your controller you can check if checkbox checked or no, then set scenario. Something like that

      if($_POST['my_checbox']==1)
       $model->setscenario('checked');  
    

    Then just do $model->validate() to check for errors. In your model rules just set validators for scenarios you have:

    array('delivery, firstNameBilling, lastNameBilling, addressBilling, cityBilling, countryBilling,
                postBilling, telephoneBilling, mailBilling, firstNameDelivery, lastNameDelivery, addressDelivery,
                cityDelivery, countryDelivery, postDelivery, telephoneDelivery, mailDelivery', 'required','on'=>'checked'),
    

    Thats all. Pretty simple.

    0 讨论(0)
提交回复
热议问题