5

我在徘徊是否有机会使用场景作为规则,

在我的模型中,我有

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>:&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 包含计费字段,而隐藏的包含交付字段。

当我想提交表单并且未选中复选框时,我不能这样做,因为必填字段,所以我在徘徊是否有机会针对这种情况使用场景以及如何使用,我需要一个线索。

谢谢,希望你能理解我的问题。

4

1 回答 1

10

是的,有可能。在您的控制器中,您可以检查复选框是否选中,然后设置场景。类似的东西

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

就这样。很简单。

于 2013-07-02T08:13:24.297 回答