Codeigniter 3应用程序错误:添加is_unique验证条件可防止提交编辑表单数据

时间:2020-06-07 13:14:21

标签: php codeigniter codeigniter-3

我正在使用Codeigniter 3.1.8和Bootstrap 4开发基本的 blog application 。该应用程序具有用户(作者)帐户。

登录后,作者可以编辑他/她的帐户信息,包括电子邮件地址:

在电子邮件字段中添加is_unique(以防止帐户信息 edit 上的重复电子邮件地址,而不仅仅是创建)会导致以下错误:

当我尝试使用已经分配了作者的电子邮件(包括我自己)更新电子邮件时,将显示验证错误消息电子邮件已被接收

在控制器中,我有以下更新方法:

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>

我的错误在哪里?

1 个答案:

答案 0 :(得分:1)

这是is_unique的工作方式,它无法检查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; 
    }
}

查看它是否对您有帮助。

相关问题