Verify form class in ci framework

Source: Internet
Author: User
This is the add method of the controller {code...}. This is the {code...} of the view. Why did I enter index. phpadminblog_cadd in the browser for the first time? It takes $ this-& amp; gt; load-& amp; gt; view (& #039; adminblogcadd & #039 ;); if this parameter is used, the default value is false... this is the add method of the controller.
Public function add () {// verify the validity $ this-> load-> library ('form _ validation '); // set the rule $ this-> form_validation-> set_rules ('title', 'title', 'required | max_length [10] '); $ this-> form_validation-> set_rules ('content', 'content', 'required | max_length [150] '); if ($ this-> form_validation-> run () = false) {$ this-> load-> view ('admin/blogc/add ');} else {// insert data $ this-> load-> model ('blogm', 'bm '); $ this-> bm-> insert (); redirect ('admin/blogc/add ');}}
This is the view
       
Problem

Why did I enter index. php/admin/blog_c/add in the browser for the first time?
It takes $ this-> load-> view ('admin/blogc/add ');
If this parameter is used, it indicates that the default value is false. However, if it is falsed for the first time, why is it not because the user name cannot be blank but must be submitted?

Reply: This is the add method of the controller.
Public function add () {// verify the validity $ this-> load-> library ('form _ validation '); // set the rule $ this-> form_validation-> set_rules ('title', 'title', 'required | max_length [10] '); $ this-> form_validation-> set_rules ('content', 'content', 'required | max_length [150] '); if ($ this-> form_validation-> run () = false) {$ this-> load-> view ('admin/blogc/add ');} else {// insert data $ this-> load-> model ('blogm', 'bm '); $ this-> bm-> insert (); redirect ('admin/blogc/add ');}}
This is the view
       
Problem

Why did I enter index. php/admin/blog_c/add in the browser for the first time?
It takes $ this-> load-> view ('admin/blogc/add ');
If this parameter is used, it indicates that the default value is false. However, if it is falsed for the first time, why is it not because the user name cannot be blank but must be submitted?

To answer this question, the first time you open the template, it must be loading your template. Why? Because you did not submit the title and content data at the first time, the verification rules here are as follows:

$ This-> form_validation-> set_rules ('title', 'title', 'required | max_length [10] '); $ this-> form_validation-> set_rules ('content ', 'content', 'required | max_length [150] ');

There is a required validation rule in it, which means that it is not empty, so when you load the add method for the first time, no data is submitted, so

$this->form_validation->run()

If the result is false, the admin/blogc/add template is loaded.
If your user name is not blank or empty, this prompt is followed by this function.

form_error();

And the validation class form_validation. If the first verification fails but no error is displayed, analyze the Code as follows:
Form_validation: This verification class verifies the data submitted by post, but it was not submitted for the first time. Therefore, although it is false, no data is obtained, so no error is returned. That is to say, when you submit a blank record for the second time, you can use $ _ POST ['title'] or $ _ POST ['content'] to obtain a title or content submission, but it is empty. However, during the first get loading, $ _ POST ['title'] or $ _ POST ['content'] does not exist at all, so it cannot be verified and false is returned directly. If you want to verify my guess, right? You can see the source code of the validation class at a glance. However, my idea is that it is always better.

An example is provided at the end.

For example, if I ask you to go to dinner, you can see that there are two bowls of rice, you can see that there is a bowl of rice you don't want to eat, but suppose you see two empty bowls, how do you eat? Here is the so-called first loading of nothing, but will return false; you see two bowls before you eat, see a bowl you do not eat, see two bowls before you eat, this is the case where a meal exists. If it is empty, it is equivalent to a bowl of rice. If the verification fails, false is reported, and the cause of the error is prompted. The first time there is no meal, it is not considered, so although you don't want to eat it, you can report it. That's the case. Of course, you can replace the meal with shit to increase your impression.

Because the first open request is a get request and the second is a post request.

On the contrary, CI can only verify post requests by default. The first section of the run () method is written as follows:

// Do we even have any data to process?  Mm?$validation_array = empty($this->validation_data) ? $_POST : $this->validation_data;if (count($validation_array) === 0){   return FALSE;}

Therefore, we can post on $ this-> validation_data. In fact, CI provides the public function set_data (array $ data) method to manually add the data to be verified:

public function set_data(array $data)    {        if ( ! empty($data))        {            $this->validation_data = $data;        }        return $this;    }

Or my sentence: The first time you open this URL is a get request and the second is a post request. By default, only post requests are verified, unless you manually call the set_data () method.

This verification of CI is indeed different from the conventional one. It is normal to first determine whether it is a post request, and then verify it. ci does not need, by default, if there is no _ POST request data, the verification is false, but the error message is null. Note that the error message is empty, rather than prompting that the parameter does not exist. So what you said will happen.

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.