doubo4824 2018-06-28 01:45
浏览 61

相同的Jquery验证应用于注册和更新,但在远程方法上发送电子邮件问题

I am using CodeIgniter and jQuery validation. I have a registration form in that fields are Firstname, Lastname, email, mobile. Jquery validation is working perfectly. I am using the remote method to check the email id is existed or not in the database which also works. There is no issue in the form while registration.

Come to the update records

I am using the same jquery validation for both the form "Registration" and "Update". Even validation is also working but there is some issue in the remote method.

I haven't updated my email and then click on submit button it's showing "Email id already exist". AS per the process, this is the right but is there any other way, without update the email remote method should not call? or if the user updates the email field then remote method call for an update? or else should I check email before updating the record? or should I use separate validation for an update?

What is the best way to use? Any other idea?

Jquery Validation

$("#emp_register").validate({
        // Specify the validation rules
        rules: {
           firstname:{
                 required: true,
                 minlength:3,
                maxlength:50
           },
          lastname:{
                 required: true,
                 minlength:3,
                maxlength:50
           },
            email: {
                required: true,
                 email:true,
                 remote: {
                    url: baseUrl + "/Employee_control/checkemail_exist",
                    type: "post",
                    data: {
                          email: function () {
                               return $("#email").val();
                        }}
                 }
            },
          mobileno: {
                required: true,
                number:true,
                minlength:10,
                maxlength: 10

            }
   },
      messages: {
        email: {remote: "This email is already registered!"
      }
    },
         submitHandler: function(form) {
         form.submit();
         }
    });

Registration

public function employee_register(){
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');

        $this->form_validation->set_rules('firstname', 'Firstname', 'trim|required|min_length[3]|max_length[25]');
        $this->form_validation->set_rules('lastname', 'Lastname', 'trim|required|min_length[3]|max_length[25]');
        $this->form_validation->set_rules('email', 'Email', 'required');
        $this->form_validation->set_rules('mobileno', 'Mobile no', 'trim|required|regex_match[/^[0-9]{10}$/]');
        if ($this->form_validation->run() == FALSE)
                {
                      $this->index();
                 }
                 else
                {
        $data = array(
                'firstname'=>$this->input->post('firstname');,
                'lastname'=>$this->input->post('lastname'),
                'email_id'=>$this->input->post('email'),
                'mobileno'=>$this->input->post('mobileno'),
                 );
              $secure_data = $this->security->xss_clean($data);
               $this->db->insert('tbl_employee',$secure_data);  
               redirect("Employee_control/index");   
          }

    }

Update

public function update_employee_info(){
    $update_key_id=$this->encryption->decrypt(base64_decode($this->input->get('key')));

    $this->form_validation->set_error_delimiters('<div class="error">', '</div>');

    $this->form_validation->set_rules('firstname', 'Firstname', 'trim|required|min_length[3]|max_length[25]');
    $this->form_validation->set_rules('lastname', 'Lastname', 'trim|required|min_length[3]|max_length[25]');
   $this->form_validation->set_rules('email', 'Email', 'required');
    $this->form_validation->set_rules('mobileno', 'Mobile no', 'trim|required|regex_match[/^[0-9]{10}$/]');
           if ($this->form_validation->run() == FALSE)
                {
                     $this->load->view('employee/employee_edit');
                 }
                 else
                {

    $updateData = array(
                'firstname'=>$this->input->post('firstname'),
                'lastname'=>$this->input->post('lastname'),
                'email_id'=>$this->input->post('email'),
                'mobileno'=>$this->input->post('mobileno')
              );
           $secure_updateData = $this->security->xss_clean($updateData);
            $this->db->where('id', $update_key_id);
            $this->db->update('tbl_employee', $secure_updateData);   
            redirect("Employee_control/employee_list"); 
                }
}
  • 写回答

1条回答 默认 最新

  • douke6424 2018-06-28 03:18
    关注

    I assume that there is an <input> (perhaps hidden) on the registration form named "key" like there is on the update form. On the registration form the value is probably blank or an empty string.

    If that's true one solution might be to pass the email and key values in the JQuery validation of email.

    For example this might work:

    data:{
        key: function () {
        return $("#key").val();
        },
        email: function () {
        return $("#email").val();
        }
    },
    

    The checkemail_exist() method can use the key to determine if this is an update or insert situation. I have made up a model and some modle->methods since you don't share those. Hopefully the intention for these is clear.

    public function checkemail_exist()
    {
        $key = $this->input->get('key');  
        $email = $this->input->get('email');
    
        if(!empty($key)) //then this is an update
        {
            // This would be the place to decrypt $key if needed
    
            // get the email associated with the user
            $existing_email = $this->db->user_model->get_email($key);
    
            if($email == $existing_email)
            {
                //the email input is unchanged from a previously validated value
                return true;
            }
        }
    
        //With either a new user or exiting user who has changed their email
        //the new input must be unique
        //For example, this made up model->method returns true if the email isn't on file
        return $this->user_model->check_unique_email($email);
    }
    

    If you are using the jQuery validation plugin I think you are I understand the return needs to be more than a simple boolean like is done above. I'll leave that implementation detail to you.

    评论

报告相同问题?