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.