Php yii中验证规则的场景
我在想有没有可能用场景来设定规则 在我的模型中,我有Php yii中验证规则的场景,php,validation,yii,scenarios,yii-validation,Php,Validation,Yii,Scenarios,Yii Validation,我在想有没有可能用场景来设定规则 在我的模型中,我有 public function rules() { return array( array('delivery, firstNameBilling, lastNameBilling, addressBilling, cityBilling, countryBilling, postBilling, telephoneBilling, mailBilling, firstNameDelivery,
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),
);
}
在我看来,我有这样的想法
<tr>
<td>
<p><?php echo $form->label($model,'telephoneBilling'); ?><span>: </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>: </span><span class="required">*</span></p>
</td>
<td>
<?php echo $form->textField($model,'firstNameDelivery'); ?>
<?php echo $form->error($model,'firstNameDelivery'); ?>
</td>
:*
送货信息:如果您的送货地址与账单地址不同,请勾选此框,然后输入
在下面提供的字段中输入所需的送达地址
:*
这只是给你一张我所做的图片的一部分,所以当我点击复选框时,我会显示这个隐藏的div,他对他的字段有一个规则(第一个div包含账单字段,隐藏的包含递送字段)
当我想提交表单,但复选框未选中时,由于必填字段,我无法提交表单,因此我在漫游
是否有机会在这种情况下使用场景以及如何使用,我需要一个线索
谢谢,我希望你能理解我的问题。是的,这是可能的。在你的控制器中,你可以检查复选框是否选中,然后设置场景。类似的事情
if($_POST['my_checbox']==1)
$model->setscenario('checked');
然后只需执行$model->validate()
即可检查错误。
在模型规则中,只需为您的场景设置验证器:
array('delivery, firstNameBilling, lastNameBilling, addressBilling, cityBilling, countryBilling,
postBilling, telephoneBilling, mailBilling, firstNameDelivery, lastNameDelivery, addressDelivery,
cityDelivery, countryDelivery, postDelivery, telephoneDelivery, mailDelivery', 'required','on'=>'checked'),
仅此而已。非常简单。谢谢伊尔萨,你帮了我很多忙:)场景使用案例的好例子太棒了。很好的用例:)