Codeigniter 3 应用程序错误:添加 is_unique 验证条件会阻止提交编辑表单数据
Codeigniter 3 application bug: adding is_unique validation criteria prevents edit form data from submitting
我正在使用 Codeigniter 3.1.8 和 Bootstrap 进行基础 blog application 4. 该应用程序具有用户(作者)帐户。
登录后,作者可以编辑his/her帐户信息,包括电子邮件地址:
将 is_unique
添加到电子邮件字段(为了防止帐户信息中的重复电子邮件地址 编辑,而不仅仅是创建)导致此错误:
当我尝试使用已指定作者的电子邮件更新电子邮件时,包括我自己,验证错误消息电子邮件已被使用出现。
在控制器中我有这个更新方法:
public function update() {
// Only logged in users can update user profiles
if (!$this->session->userdata('is_logged_in')) {
redirect('login');
}
$id = $this->input->post('id');
$data = $this->Static_model->get_static_data();
$data['pages'] = $this->Pages_model->get_pages();
$data['categories'] = $this->Categories_model->get_categories();
$data['author'] = $this->Usermodel->editAuthor($id);
$this->form_validation->set_rules('first_name', 'First name', 'required');
$this->form_validation->set_rules('last_name', 'Last name', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email|is_unique[authors.email]');
$this->form_validation->set_message('is_unique', 'The %s is already taken');
$this->form_validation->set_error_delimiters('<p class="error-message">', '</p>');
// Upload avatar
$config['upload_path'] = './assets/img/authors';
$config['allowed_types'] = 'jpg|jpeg|png';
$config['max_size'] = '1024';
$this->load->library('upload', $config);
if (!$this->upload->do_upload('userfile')) {
$uerrors = array('uerrors' => $this->upload->display_errors());
// if NO file is uploaded,
if (empty($_FILES['userfile']['name'])) {
// force upload validation and
$uerrors = [];
// use the existing avatar (if any)
$avatar = $this->input->post('avatar');
}
$data['uerrors'] = $uerrors;
} else {
$data = array('upload_data' => $this->upload->data());
$avatar = $_FILES['userfile']['name'];
$this->session->set_userdata('user_avatar', $avatar);
}
if(!$this->form_validation->run() || !empty($uerrors)) {
$this->load->view('partials/header', $data);
$this->load->view('dashboard/edit-author');
$this->load->view('partials/footer');
} else {
$this->Usermodel->update_user($avatar, $id);
$this->session->set_flashdata('user_updated', 'Your account details have been updated');
redirect(base_url('/dashboard/manage-authors'));
}
}
在带有编辑表单的视图中:
<div class="form-group <?php if(form_error('email')) echo 'has-error';?>">
<input type="text" name="email" id="email" class="form-control" value="<?php echo set_value('email', $author->email); ?>" placeholder="Email">
<?php if(form_error('email')) echo form_error('email'); ?>
</div>
我的错误在哪里?
这就是 is_unique
的工作原理,它检查 table 是否有任何重复的条目,因为它无法知道 id
正在编辑什么。这是一个错误吗?也许吧。
但是您可以创建自己的验证函数并调用它。像这样 -
public function update() {
$id = $this->input->post('id');
// ...
// ...
// ...
$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email|callback__checkemail['.$id.']'); // pass id
// ...
// ...
// ...
}
function _checkemail($email, $id){
// don't need to $this->input->post('email') as it is a validation for the same ↑↑(set_rules), if you need anything else then $this->input->post('input_field')
if( !$id ){
$this->db->where('email', $email);
$num = $this->db->count_all_results('authors');
}else{
$this->db->where('email', $email);
$this->db->where_not_in('id', $id); // not in current id in edit mode
$num = $this->db->count_all_results('authors');
}
// You'd probably want to do DB queries↑↑ in model
// You can also combine the queries into one as they seem repetitive
if ($num > 0) {
$this->form_validation->set_message('_checkemail','Email already exists!'); //set message to the callbacked function(ie _checkemail) not the input field here.
return FALSE;
} else {
return TRUE;
}
}
看看对你有没有帮助。
我正在使用 Codeigniter 3.1.8 和 Bootstrap 进行基础 blog application 4. 该应用程序具有用户(作者)帐户。
登录后,作者可以编辑his/her帐户信息,包括电子邮件地址:
将 is_unique
添加到电子邮件字段(为了防止帐户信息中的重复电子邮件地址 编辑,而不仅仅是创建)导致此错误:
当我尝试使用已指定作者的电子邮件更新电子邮件时,包括我自己,验证错误消息电子邮件已被使用出现。
在控制器中我有这个更新方法:
public function update() {
// Only logged in users can update user profiles
if (!$this->session->userdata('is_logged_in')) {
redirect('login');
}
$id = $this->input->post('id');
$data = $this->Static_model->get_static_data();
$data['pages'] = $this->Pages_model->get_pages();
$data['categories'] = $this->Categories_model->get_categories();
$data['author'] = $this->Usermodel->editAuthor($id);
$this->form_validation->set_rules('first_name', 'First name', 'required');
$this->form_validation->set_rules('last_name', 'Last name', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email|is_unique[authors.email]');
$this->form_validation->set_message('is_unique', 'The %s is already taken');
$this->form_validation->set_error_delimiters('<p class="error-message">', '</p>');
// Upload avatar
$config['upload_path'] = './assets/img/authors';
$config['allowed_types'] = 'jpg|jpeg|png';
$config['max_size'] = '1024';
$this->load->library('upload', $config);
if (!$this->upload->do_upload('userfile')) {
$uerrors = array('uerrors' => $this->upload->display_errors());
// if NO file is uploaded,
if (empty($_FILES['userfile']['name'])) {
// force upload validation and
$uerrors = [];
// use the existing avatar (if any)
$avatar = $this->input->post('avatar');
}
$data['uerrors'] = $uerrors;
} else {
$data = array('upload_data' => $this->upload->data());
$avatar = $_FILES['userfile']['name'];
$this->session->set_userdata('user_avatar', $avatar);
}
if(!$this->form_validation->run() || !empty($uerrors)) {
$this->load->view('partials/header', $data);
$this->load->view('dashboard/edit-author');
$this->load->view('partials/footer');
} else {
$this->Usermodel->update_user($avatar, $id);
$this->session->set_flashdata('user_updated', 'Your account details have been updated');
redirect(base_url('/dashboard/manage-authors'));
}
}
在带有编辑表单的视图中:
<div class="form-group <?php if(form_error('email')) echo 'has-error';?>">
<input type="text" name="email" id="email" class="form-control" value="<?php echo set_value('email', $author->email); ?>" placeholder="Email">
<?php if(form_error('email')) echo form_error('email'); ?>
</div>
我的错误在哪里?
这就是 is_unique
的工作原理,它检查 table 是否有任何重复的条目,因为它无法知道 id
正在编辑什么。这是一个错误吗?也许吧。
但是您可以创建自己的验证函数并调用它。像这样 -
public function update() {
$id = $this->input->post('id');
// ...
// ...
// ...
$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email|callback__checkemail['.$id.']'); // pass id
// ...
// ...
// ...
}
function _checkemail($email, $id){
// don't need to $this->input->post('email') as it is a validation for the same ↑↑(set_rules), if you need anything else then $this->input->post('input_field')
if( !$id ){
$this->db->where('email', $email);
$num = $this->db->count_all_results('authors');
}else{
$this->db->where('email', $email);
$this->db->where_not_in('id', $id); // not in current id in edit mode
$num = $this->db->count_all_results('authors');
}
// You'd probably want to do DB queries↑↑ in model
// You can also combine the queries into one as they seem repetitive
if ($num > 0) {
$this->form_validation->set_message('_checkemail','Email already exists!'); //set message to the callbacked function(ie _checkemail) not the input field here.
return FALSE;
} else {
return TRUE;
}
}
看看对你有没有帮助。