Analysis on form validation of CI framework

不言
Release: 2023-04-01 07:26:02
Original
1675 people have browsed it

This article mainly introduces the CI framework form verification method. It summarizes and analyzes the CI framework form verification rule settings, error prompts, view display and other related operating techniques and usage methods in the form of examples. Friends in need can refer to it

The example in this article describes the CI framework form verification method. Share it with everyone for your reference, the details are as follows:

1. Automatic output function of form header information (view)

 'email', 'id' => 'myform');
 echo form_open('email/send', $attributes);
 //上面一行代码输出:
 //
Copy after login

2. Set verification rules (controller)

 'username',
           'label'  => '用户名',
           'rules'  => 'required'
         ),
        array(
           'field'  => 'password',
           'label'  => '密码',
           'rules'  => 'required'
         ),
        array(
           'field'  => 'passconf',
           'label'  => '确认密码',
           'rules'  => 'required|matches[password]'
         ),
        array(
           'field'  => 'tel',
           'label'  => '手机',
           'rules'  => 'required|integer|exact_length[11]'),
        array(
           'field'  => 'email',
           'label'  => '邮箱',
           'rules'  => 'required|valid_email'
         )
      );
//上面的会自动
//单独设置规则
$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_rules('email', 'Email', 'trim|required|valid_email');
?>
Copy after login

3. Error prompts corresponding to the rules (controller)

form_validation->set_message('required', '必须填写');
$this->form_validation->set_message('valid_email', '不是有效的email');
?>
Copy after login

4. Run check error message (controller)

load->helper(array('form', 'url'));
 //加载CI表单验证库
 $this->load->library('form_validation');
 //----------------------------------------
 # 验证规则及错误信息代码放在这里
 //----------------------------------------
 if ($this->form_validation->run() == FALSE){
   //提交失败 重新加载表单部分
   $this->load->view('myform');
 }else{
   //提交成功 表单处理
   //跳转成功页面
   $this->load->view('formsuccess');
 }
}
Copy after login

5. Error message output function (view)

form_validation->set_error_delimiters('', '');
  //设置成内联元素比较好
?>
Copy after login

6. Refill the form (view) after an error

Copy after login

html code:


 
  My Form
 

 
 
  
Username
Password
Password Confirm
Email Address

Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

About common image processing methods encapsulated in CI framework

About loading views in CI framework views method

The above is the detailed content of Analysis on form validation of CI framework. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!