Yii2:使用相同模型的两个实例进行表单验证 [英] Yii2: Validation in form with two instances of same model

查看:20
本文介绍了Yii2:使用相同模型的两个实例进行表单验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型 Booking,其中包含字段 shipping_address 和 billing_address,这两个都是模型地址.我现在想将它们打印到基本工作得很好的相同表单,问题是两个模型的字段具有相同的 ID.例如,shipping_address 中的字段zip"的 id 为zip",而 billing_address 中的zip"的 id 也为zip".

I have a model Booking with the fields shipping_address and billing_address, both of the model Address. I now want to print them to the same form which basically works pretty well, the problem is the fields of both models have the same IDs. For example the field "zip" in the shipping_address has the id "zip" and the "zip" in billing_address also has "zip" as its id.

如果我现在填写表单,它会同时验证两个字段,而实际上只有一个字段是正确的.

If I now fill out the form, it validates both of the fields at the same time while only one of them is actually true.

我已经将字段的名称更改为 Shippping_Address['zip'] 和 Billing_Address['zip'],因此控制器能够识别这两种模型.

I've already changed the names of the fields to Shippping_Address['zip'] and Billing_Address['zip'] therefore the controller is able to recognize both models.

以下是字段的打印方式:

Here's how the fields are printed:

<?= $form->field($billing_address, 'address_line_1')->textInput(['maxlength' => 45, 'name'=> 'Billing_Address[address_line1]']); ?>
<?= $form->field($billing_address, 'address_line_2')->textInput(['maxlength' => 45, 'name'=> 'Billing_Address[address_line2]']) ?>
<?= $form->field($billing_address, 'zip')->textInput(['maxlength' => 11, 'name'=> 'Billing_Address[zip]']) ?>
<?= $form->field($billing_address, 'city')->textInput(['maxlength' => 45, 'name'=> 'Billing_Address[city]']) ?>
<?= $form->field($billing_address, 'country')->textInput(['maxlength' => 45, 'name'=> 'Billing_Address[country]']) ?>

<?= $form->field($shipping_address, 'address_line_1')->textInput(['maxlength' => 45, 'name'=> 'Shipping_Address[address_line1]']); ?>
<?= $form->field($shipping_address, 'address_line_2')->textInput(['maxlength' => 45, 'name'=> 'Shipping_Address[address_line2]']) ?>
<?= $form->field($shipping_address, 'zip')->textInput(['maxlength' => 11, 'name'=> 'Shipping_Address[zip]']) ?>
<?= $form->field($shipping_address, 'city')->textInput(['maxlength' => 45, 'name'=> 'Shipping_Address[city]']) ?>
<?= $form->field($shipping_address, 'country')->textInput(['maxlength' => 45, 'name'=> 'Shipping_Address[country]']) ?>

执行托尼的回答后:

<?= $form->field(
            $shipping_address,
            'address_line_1',
            [
                'selectors' => [
                    'input' => '#shipping-address_line_1',
                    'container' => '.shipping-address_line_1'
                ],
                'options' =>
                    ['class' => 'shipping-address_line_1']
            ])->textInput([
                'maxlength' => 45,
                'name'=> 'Shipping_Address[address_line1]',
                'id'=>'shipping-address_line_1',
            ]); ?>

我还更改了容器类,因为它与另一个字段的容器类相同.现在第二个字段(代码如上所示)运行良好.问题是,第一个字段(代码不变)不再验证.

I also changed the container class because it was the same as the one the other field had. Now the second field (with the code as shown above) is working perfectly. The problem is, the first field (with the code unchanged) is not validating anymore.

如何解决?

推荐答案

因为您使用一个模型类在一页上生成两组输入,所以 yii2 为它们生成相同的 clientValidation 规则.要单独验证,您需要为每个需要手动验证的输入设置 id 属性.对于您的 zip 输入字段,解决方案将是下一个(根据最新评论进行编辑):

Because you use one model class for generating two sets of inputs at one page, yii2 generates identical clientValidation rules for both of them. To separate validation you need to set id attribute for each input that required validation in each of this sets manually. For your zip input field the solution will be next (edited according to latest comments):

//Billing
$form->field($billing_address, 'zip', 
[
    'selectors' => [
        'input' => '#billing-zip',
        'container' => '#billing-container',
    ],
    'options' => ['id' => 'billing-container'],
])->textInput(['maxlength' => 11, 
               'name'=> 'Billing_Address[zip]', 
               'id'=>'billing-zip']); 

//Shipping
$form->field($shipping_address, 'zip', 
[
    'selectors' => [
        'input' => '#shipping-zip',
        'container' => '#shipping-container',
    ],
    'options' => ['id' => 'shipping-container'],
])->textInput(['maxlength' => 11, 
               'name'=> 'Shipping_Address[zip]', 
               'id'=>'shipping-zip']);

如您所见,我们在 textInput 选项中添加了自定义 id 属性,并在 field 选项中为 js 验证设置了相应的选择器.阅读选择器属性

As you can see we added custom id attribute in textInput options, and set corresponding selector for js validation in field options. Read about selectors property

这篇关于Yii2:使用相同模型的两个实例进行表单验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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