scenario for validation rules in yii

╄→尐↘猪︶ㄣ 提交于 2019-11-30 05:30:08

问题


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

in my model I have

public function rules()
{
    return array(
        array('delivery, firstNameBilling, lastNameBilling, addressBilling, cityBilling, countryBilling,
            postBilling, telephoneBilling, mailBilling, firstNameDelivery, lastNameDelivery, addressDelivery,
            cityDelivery, countryDelivery, postDelivery, telephoneDelivery, mailDelivery', 'required'),
        array('active', 'numerical', 'integerOnly'=>true),
    );
}

and in my view I have something like this

    <tr>
        <td>
            <p><?php echo $form->label($model,'telephoneBilling'); ?><span>:&nbsp;</span><span class="required">*</span></p>
        </td>
        <td>
            <?php echo $form->textField($model,'telephoneBilling'); ?>
            <?php echo $form->error($model,'telephoneBilling'); ?>
        </td>
    </tr>
</table>

<p><?php echo $form->checkBox($model,'active', array('class' => 'change')); ?>
    Delivery information: Please check the box if your delivery address differs from your billing address and enter the
    required delivery address in the fields provided below.</p>

    <div id="deliveryFormWrapper" style="display: none">
    <table class="cartReviewTable">
    <tr>
        <td colspan="4">
            <span class="blueTitle"><?php echo CHtml::encode(Yii::t('app', 'Delivery Information ')); ?></span>
        </td>
    </tr>
    <tr>
        <td>
            <p><?php echo $form->label($model,'firstNameDelivery'); ?><span>:&nbsp;</span><span class="required">*</span></p>
        </td>
        <td>
            <?php echo $form->textField($model,'firstNameDelivery'); ?>
            <?php echo $form->error($model,'firstNameDelivery'); ?>
        </td>

This is just a part to give you a picture what i do, so when i click on checkbox i show this hidden div, and he has a rules for his fields (the first div contains Billing fields, and the hidden contains delivery fields.

When i want to submit the form and the checkbox is not selected, i cant do it, because of required fields, so i was wandering Is there any chance to use scenario for that situation and how, i need a clue.

Thanks, i hope you can understand my question.


回答1:


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.



来源:https://stackoverflow.com/questions/17420129/scenario-for-validation-rules-in-yii

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!