yii中验证规则的方案 [英] scenario for validation rules in yii

查看:102
本文介绍了yii中验证规则的方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在流浪,有没有机会使用场景作为规则,

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

仅此而已.很简单.

这篇关于yii中验证规则的方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆