Yii2:验证形式与同型号的两个实例 [英] Yii2: Validation in form with two instances of same model

查看:235
本文介绍了Yii2:验证形式与同型号的两个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型,预定与模型地址字段shipping_address和billing_address,两者兼而有之。 现在我想将它们打印到相同的形式,基本工作原理pretty的很好,问题是这两种模式的字段具有相同的ID。例如,在shipping_address领域的拉链ID为压缩和压缩在billing_address也有压缩为ID。

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 ['拉链']和Billing_Address ['拉链']因此,控制器能够识别这两种模式。

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]']) ?>

编辑:实施托尼的回答后:

After implementing the answer of tony:

<?= $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',
            ]); ?>

我也改变了容器类,因为它是同一个其他领域了。现在第二个字段(如上图所示的code)的正常使用。问题是,在第一场(与code不变)未验证了。

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

正如你所看到的,我们添加了自定义的 ID 的属性中的与textInput 的选项,并设置相应的选择中的字段的选项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天全站免登陆