Can a specification corresponding to a rules in Yii2 map the rules of another model?

Source: Internet
Author: User
The following may be the form data to be submitted {code ...} I intend to write an OrderFormmodel to verify the data. products corresponds to a model, and there is also a model for Form Verification marketing, there is also a Form Verification OrderForm rules should... the following may be the form data to be submitted:

phpArray(    [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)

I plan to write an OrderForm model to verify the data;
Products corresponds to a model with form verification.
Marketing also corresponds to a model with form verification.

How to write the rules of OrderForm to implement the rules of the products variable corresponding to the products model;
The marketing variable corresponds to the rules in the marketing model;

Reply content:

The following may be the form data to be submitted:

phpArray(    [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)

I plan to write an OrderForm model to verify the data;
Products corresponds to a model with form verification.
Marketing also corresponds to a model with form verification.

How to write the rules of OrderForm to implement the rules of the products variable corresponding to the products model;
The marketing variable corresponds to the rules in the marketing model;

First, create the required Model, and add the 'attribute that will save the value submitted by the form' and the corresponding verification rules/scenarios.
Load the Model to the view and specify the Model corresponding to the attribute when creating each form item, for example:$form->field($marketModel, 'market')->textinput();
Submit the form and follow the steps below to load, verify, and save it.

Refer to the steps in the official documentation
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.