Detailed description of CI framework form verification instances and ci framework form instances
This article describes the CI framework form verification method. We will share this with you for your reference. The details are as follows:
1. Automatic output function of form header information (view)
<? Php $ attributes = array ('class' => 'email ', 'id' => 'myform'); echo form_open ('email/send', $ attributes ); // The above line of code output: // <form method = "post" accept-charset = "UTF-8" action = "http://example.com/index.php/email/send" id = "myform" class = "email"/>/** form_open_multipart () * function usage is the same as above. The file upload information upload method is post by default */?>
2. Set verification rules (controller)
<? Php // note that the variable name of the validation rule must be set to config $ config = array ('field '=> 'username', 'label' => 'username ', 'rules' => 'requestred'), array ('field' => 'Password', 'label' => 'Password', 'rules' => 'required '), array ('field' => 'passconf', 'label' => 'Confirm password', 'rules' => 'required | matches [password] '), array ('field' => 'tel', 'label' => 'phone', 'rules' => 'required | integer | exact_length [11] '), array ('field' => 'email ', 'l Abel '=> 'mailbox', 'rules' => 'required | valid_email ')); // The above rule will be set automatically // separate the rule $ this-> form_validation-> set_rules ('username', 'username ', 'trim | required | min_length [5] | max_length [12] | xss_clean '); $ this-> form_validation-> set_rules ('Password', 'Password ', 'trim | required | matches [passconf] | md5'); $ this-> form_validation-> set_rules ('passconf', 'password Confirmation ', 'trim | required '); $ this-> form_validation-> set_rule S ('email ', 'email', 'trim | required | valid_email ');?>
3. error message corresponding to the rule (controller)
<? Php $ this-> form_validation-> set_message ('required', 'required?'); $ this-> form_validation-> set_message ('valid _ mail ', 'invalid email');?>
4. Running check error message (controller)
<? Php $ this-> load-> helper (array ('form', 'url ')); // load the CI form Verification library $ this-> load-> library ('form _ validation '); // ---------------------------------------- # Check the rules and error code here // -------------------------------------------- if ($ this-> form_validation-> run () = FALSE) {// failed to submit and reload the form section $ this-> load-> view ('myform ');} else {// successful submission form processing // jump to the success page $ this-> load-> view ('formsuccess ');}}
5. error message output function (view)
<? Php // 1. echo validation_errors (); // 2. echo form_error ('Password') for individual output of a single form (the parameter placed near a single tag is the domain name of the corresponding form element); // 3. when outputting a single form, you need to modify the format of the error message displayed by the delimiter (set in the Controller) $ this-> form_validation-> set_error_delimiters ('<span class = "error"> ', '</span>'); // is it better to set it to an inline element?>
6. Refill the form (view) after an error)
<? Php // general element backfilling (output in the tag's values attribute) echo set_value ('e-mail '); // special element select/checkbox/radio when the third parameter is true, it is selected by default // The second parameter is the actual value of the corresponding form Element echo set_select ('myselect ', 'Three '); // echo set_checkbox ('mycheck []', '1') in the blank attribute of option '); // echo set_radio ('myradio ', '2') in the blank attribute of the checkbox; // in the blank attribute of radio?>
Html code: