我正在使用 CodeIgniter 3.1.3 构建博客,我在管理员中有一个功能可以添加新用户。当我尝试添加新用户时,出现以下错误:
密码字段与 confirm_password 字段不匹配。
尽管 password 和 confrom password 字段中给定的密码是相同的。
我不知道是什么原因导致了这个问题。
这是我的模型:
public function insert($data){
$this->db->insert('users', $data);
return true;
}
这是我的控制器
public function add(){
//Validation Rules
$this->form_validation->set_rules('first_name','First Name','trim|required');
$this->form_validation->set_rules('last_name','Last Name','trim|required');
$this->form_validation->set_rules('email','Email','trim|required|valid_email');
$this->form_validation->set_rules('username','Username','trim|required|min_length[3]');
$this->form_validation->set_rules('password', 'Password', 'required|matches[confirm_password]');
$data['groups'] = $this->User_model->get_groups();
if($this->form_validation->run() == FALSE){
//Views
$data['main_content'] = 'admin/users/add';
$this->load->view('admin/layouts/main', $data);
} else {
//Create Data Array
$data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'username' => $this->input->post('username'),
'password' => md5($this->input->post('password')),
'group_id' => $this->input->post('group'),
'email' => $this->input->post('email')
);
//Table Insert
$this->User_model->insert($data);
//Create Message
$this->session->set_flashdata('user_saved', 'User has been saved');
//Redirect to pages
redirect('admin/users');
}
这是我的观点:
<div class="row">
<div class="col-lg-12">
<div class="form-group">
<label>First Name</label>
<input class="form-control" type="text" name="first_name" value="<?php echo set_value('first_name'); ?>" placeholder="Enter First Name" />
</div>
<div class="form-group">
<label>Last Name</label>
<input class="form-control" type="text" name="last_name" value="<?php echo set_value('last_name'); ?>" placeholder="Enter Last Name" />
</div>
<div class="form-group">
<label>Email Address</label>
<input class="form-control" type="email" name="email" value="<?php echo set_value('email'); ?>" placeholder="Enter Email" />
</div>
<div class="form-group">
<label>Username</label>
<input class="form-control" type="text" name="username" value="<?php echo set_value('username'); ?>" placeholder="Enter Username" />
</div>
<div class="form-group">
<label>Password</label>
<input class="form-control" type="password" name="password" value="<?php echo set_value('password'); ?>" placeholder="Enter Password" />
</div>
<div class="form-group">
<label>Confirm Password</label>
<input class="form-control" type="password" name="confirm_password" value="<?php echo set_value('confirm_password'); ?>" placeholder="Confirm Password" />
</div>
<div class="form-group">
<label>User Group</label>
<select name="group" class="form-control">
<?php foreach($groups as $group) : ?>
<option value="<?php echo $group->id; ?>"><?php echo $group->name; ?></option>
<?php endforeach; ?>
</select>
</div>
</div><!-- /.row -->
原文由 PapT 发布,翻译遵循 CC BY-SA 4.0 许可协议
将您的表单验证更改为此,并记住在 config/autoload.php 文件中自动加载表单验证,如
$autoload['libraries'] = array('form_validation');
。如果它不起作用,请在下面发表评论。