首页 > 后端开发 > php教程 > Yii2 中一个 rules 对应的一条规格 可以映射另外一个 model 的 rules 吗?

Yii2 中一个 rules 对应的一条规格 可以映射另外一个 model 的 rules 吗?

WBOY
发布: 2016-06-06 20:37:04
原创
908 人浏览过

下面可能是要提交的表单数据

<code>php</code><code>Array
(
    [products] => Array
        (
            [0] => Array
                (
                    [product_id] => 12
                    [product_sku] => 5614546542
                    [quantity] => 1
                    [price] => 34.00
                )

            [1] => Array
                (
                    [product_id] => 12
                    [product_sku] => 5614546542
                    [quantity] => 2
                    [price] => 34.00
                )

            [2] => Array
                (
                    [product_id] => 12
                    [product_sku] => 5614546542
                    [quantity] => 1
                    [price] => 34.00
                )

        )

    [marketing] => Array
        (
            [0] => Array
                (
                    [coupon] => 34sdhs56aj45dh56
                    [provider] => 1
                )

            [1] => Array
                (
                    [coupon] => 33454545324236
                    [provider] => 2
                )

        )

    [shop_id] => 100
    [member_card] => 3423432423123
    [submittime] => 2015-2-3 9:52:09
)
</code>
登录后复制
登录后复制

我打算是写一个 OrderForm model 验证数据;
products 对应的是一个 model, 里面也有表单验证
marketing 对应的也是一个 model, 里面也有表单验证

OrderForm 的 rules 应该怎么写才能实现 products 变量对应 products model 里面的 rules;
marketing 变量对应 marketing model 里面的 rules;

回复内容:

下面可能是要提交的表单数据

<code>php</code><code>Array
(
    [products] => Array
        (
            [0] => Array
                (
                    [product_id] => 12
                    [product_sku] => 5614546542
                    [quantity] => 1
                    [price] => 34.00
                )

            [1] => Array
                (
                    [product_id] => 12
                    [product_sku] => 5614546542
                    [quantity] => 2
                    [price] => 34.00
                )

            [2] => Array
                (
                    [product_id] => 12
                    [product_sku] => 5614546542
                    [quantity] => 1
                    [price] => 34.00
                )

        )

    [marketing] => Array
        (
            [0] => Array
                (
                    [coupon] => 34sdhs56aj45dh56
                    [provider] => 1
                )

            [1] => Array
                (
                    [coupon] => 33454545324236
                    [provider] => 2
                )

        )

    [shop_id] => 100
    [member_card] => 3423432423123
    [submittime] => 2015-2-3 9:52:09
)
</code>
登录后复制
登录后复制

我打算是写一个 OrderForm model 验证数据;
products 对应的是一个 model, 里面也有表单验证
marketing 对应的也是一个 model, 里面也有表单验证

OrderForm 的 rules 应该怎么写才能实现 products 变量对应 products model 里面的 rules;
marketing 变量对应 marketing model 里面的 rules;

首先创建需要的Model, 增加'将要保存有表单提交过来的值的'属性和对应的验证规则/场景.
加载Model到视图中, 在创建每个表单项时指定属性对应的Model, 如:$form->field($marketModel, 'market')->textinput();
提交表单, 然后参考下面的步骤 进行加载, 验证, 保存.

参考官方文档给出的步骤
1. instantiate model classes
2. populate the models attributes with input data
3. validate all models
4. If validation passes for all models, save them
5. If validation fails or no data has been submitted, display the form
by passing all model instances to the view

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板