Yii Framework form model submits form data in array form

Source: Internet
Author: User
Yii Framework form model submits form data in array form

According to the description in the Yii document, the general process of Yii in form processing is: create the model class corresponding to the form, set the field verification rule to create the corresponding action for form submission, process submitted content and create a form in the view. in a small project just now, if you want to use ajax to submit the form information and verify and save it, you do not want to use hidden iframe for refreshing the new submission, in addition, the validation method of model classes can be used in the action, so you can use the form array submission method. Example: form code:

After submission, you can directly use $ _ POST ['arr'] to obtain the submitted data. $ _ POST ['arr'] is: array ([0] => a [1] => B [2] => c)

Similarly, if you use the following form to submit:

$ _ POST ['arr']: Array ([3] => a [6] => B [8] => c)

Of course, two-dimensional arrays can also be submitted:

$ _ POST ['arr']: Array ([0] => Array ([name1] => a) [1] => Array ([name2] => B) [2] => Array ([name3] => c). if the key of the first sub-Array is not set, each value is added in the arr sequence when an array is generated. to save the information in an array, add a key value as follows:

$ _ POST ['arr']: Array ([a] => Array ([name1] => a1 [value1] => a2)=> Array ([name2] => b1 [value2] => b2 ))

The following is an example of submitting a form using ajax and verifying the form using the yii form model:

  1. Class LandingForm extends CFormModel
  2. {
  3. Public $ landing_title;
  4. Public $ landing_content;
  5. Public $ landing_position;
  6. Public function rules ()
  7. {
  8. Return array (
  9. Array ('logging _ title, landing_content ', 'required '),
  10. Array ('logging _ position', 'default', 'value' => ''),
  11. );
  12. }
  13. }

When setting the parameter verification method for a model class, you need to set rules for each public parameter. if there is a parameter with no rules set, after you assign a value to the model using the form value in $ _ POST, if no rule is set, the parameter value is null and the parameter submitted by the form is obtained and verified:

  1. $ Model = new LandingForm;
  2. $ Model-> attributes = $ _ POST ['form'];
  3. If ($ model-> validate ()){
  4. $ Info = $ model-> attributes;
  5. ...
  6. }

Finally, the code for the front-end form submission, using jquery:

  1. Var info = new Object ();
  2. Info = {'form [landing_title] ': landing_title,
  3. 'Form [landing_content] ': landing_content,
  4. 'Form [landing_position] ': landing_position,
  5. };
  6. Var url = "...";
  7. $. Post (url, info, function (rst ){
  8. ...
  9. });

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.