yii 中验证规则的场景

2022-01-04 00:00:00 validation php yii scenarios yii-validation

我在徘徊有没有机会用场景做规则,

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

在我的模型中我有

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>

这只是给你一张我所做的图片的一部分,所以当我点击复选框时,我会显示这个隐藏的 div,他的字段有一个规则(第一个 div 包含账单字段,隐藏的包含交付字段.

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.

推荐答案

是的,这是可能的.在您的控制器中,您可以检查复选框是否已选中,然后设置方案.类似的东西

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');  

然后只需执行 $model->validate() 来检查错误.在您的模型规则中,只需为您拥有的场景设置验证器:

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'),

仅此而已.很简单.

相关文章