1、form头部信息的自动输出函数(view)
- <?php
- $attributes = array('class' => 'email', 'id' => 'myform');
- echo form_open('email/send', $attributes);
-
-
-
-
-
-
-
-
- ?>
2、设置验证规则(controller)
- <?php
-
- $config = array(
- array(
- 'field' => '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');
-
- ?>
3、规则对应的错误提示(controller)
- <?php
- $this->form_validation->set_message('required', '必须填写');
- $this->form_validation->set_message('valid_email', '不是有效的email');
- ?>
4、运行检查错误信息(controller)
- <?php
- $this->load->helper(array('form', 'url'));
-
- $this->load->library('form_validation');
-
- # 验证规则及错误信息代码放在这里
-
- if ($this->form_validation->run() == FALSE){
-
- $this->load->view('myform');
- }else{
-
-
- $this->load->view('formsuccess');
- }
- }
5、错误信息的输出函数(view)
- <?php
-
- echo validation_errors();
-
- echo form_error('password');
-
- $this->form_validation->set_error_delimiters('<span class="error">', '</span>');
-
- ?>
6、错误后 重新回填表单(view)
<?php
//一般元素 回填(放在标签的values属性中输出)
echo set_value('email');
//特殊元素select/checkbox/radio 第三个参数为true时 默认被选中
//第二个参数 是对应的表单元素的实际值
echo set_select('myselect', 'three'); //放在option的空白属性里
echo set_checkbox('mycheck[]', '1'); //放在checkbox的空白属性里
echo set_radio('myradio', '2'); //放在radio的空白属性里
?>
【html代码】
- <html>
- <head>
- <title>My Form</title>
- </head>
- <body>
- <?php echo validation_errors(); ?>
-
- <?php echo form_open('form'); ?>
-
- <h5>Username</h5>
- <input type="text" name="username" value="<?php echo set_value('username'); ?>" size="50" />
- <?php echo form_error('username'); ?>
-
- <h5>Password</h5>
- <input type="text" name="password" value="<?php echo set_value('password'); ?>" size="50" />
- <?php echo form_error('password'); ?>
-
- <h5>Password Confirm</h5>
- <input type="text" name="passconf" value="<?php echo set_value('passconf'); ?>" size="50" />
- <?php echo form_error('passconf'); ?>
-
- <h5>Email Address</h5>
- <input type="text" name="email" value="<?php echo set_value('email'); ?>" size="50" />
- <?php echo form_error('email'); ?>
-
- <div><input type="submit" value="Submit" /></div>
-
- </form>
-
- </body>
- </html>
- 来自 http://blog.csdn.net/wujiangwei567/article/details/44588929