How to use a form to collect two or more model data?

Source: Internet
Author: User
Suppose we want to use an HTML form to collect input modelA and modelB, and the input error (if any) we want to display in the same error message box. Write the following code:

Suppose we want to use an HTML form to collect input model A and model B, and the input error (if any) we want to display in the same error message box. Write the following code:

Public function actionCreate () {$ a = new A; $ B = new B; if (isset ($ _ POST ['A'], $ _ POST ['B']) {// populate input data to $ a and $ B $ a-> attributes =$ _ POST ['A']; $ B-> attributes =$ _ POST ['B']; // validate BOTH $ a and $ B $ valid = $ a-> validate (); $ valid = $ B-> validate () & $ valid; if ($ valid) {// use false parameter to disable validation $ a-> save (false ); $ B-> save (false );//... redirect to another page }}$ this-> render ('create', array ('a' => $ a, 'B' => $ B ,));}

As the create view, we need to write down the following code,

 // Create. php

 RenderPartial ('_ form', array ('a' => $ a,' B '=> $ B);?> // _ Form. php

 ... Input fields for $ a, $ B...

We can also use more than two models to process the above method.

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.