Jeff Davidson
Jeff Davidson

Reputation: 1929

Not returning error when username or email exists

I'm trying to figure ouit why its not reporting a validation error when there is a match in my database for my username or email address.

It still shows this as the post parameters which are right. It does do two different post requests upon taking the focus off either.

Register Controller:

public function is_username_available()
{
    if ($this->users_model->is_username_available($this->input->post('username')))
    {
        return false;
    }
    else
    {
        return true;
    }        
}

public function is_email_available()
{
    if ($this->users_model->is_email_available($this->input->post('email_address')))
    {
        return false;
    }
    else
    {
        return true;
    }        
}

register.js

rules: {
        username: {
            minlength: 6,
            maxlength: 12,
            remote: {
                type: 'post',
                url: 'register/is_username_available',
                data: {
                    'username': function() { return $('#username').val(); }
                },
                dataType: 'json'
            }
        },
        email_address: {
            email: true,
            remote: {
                type: 'post',
                url: 'register/is_email_available',
                data: {
                    'email_address': function() { return $("#email_address").val(); }
                },
                dataType: 'json'
            }
        }     

Users_Model:

/**
 * Check if username available for registering
 *
 * @param   string
 * @return  bool
 */
function is_username_available($username)
{
    $this->db->select('username', FALSE);
    $this->db->where('LOWER(username)=', strtolower($username));

    $query = $this->db->get($this->users_table);
    echo $this->db->last_query();
    return $query->num_rows() == 0;
}

/**
 * Check if email available for registering
 *
 * @param   string
 * @return  bool
 */
function is_email_available($email)
{
    $this->db->select('email', FALSE);
    $this->db->where('LOWER(email)=', strtolower($email));

    $query = $this->db->get($this->users_table);
    return $query->num_rows() == 0;
}

Upvotes: 1

Views: 197

Answers (1)

csotelo
csotelo

Reputation: 1485

I think ajax not accept TRUE or FALSE on the server side, you should send a string that tells ajax you have to do. For example:

in model:

function is_username_available($username)
{
    $this->db->select('username', FALSE);
    $this->db->where('LOWER(username)=', strtolower($username));

    $query = $this->db->get($this->users_table);

    if($query->num_rows() > 0)
    {
      // no available
      return FALSE;
    }
    else
    {
      // available
      return TRUE;
    }
}

in controller:

public function is_username_available()
{
    if (!$this->users_model->is_username_available($this->input->post('username')))
    {
        // no available
        exit('n');
    }
    else
    {
        // available
        exit('y');
    }        
}

Find an Event "success" in your library jquery, and asks something like:

success: function(data){ 

      if(data == 'y')
      {
        alert('available');
      }
      else
      {
        alert('no available');
      }
}

I think the rules of the validation plugin work one by one. Then you can send multiple answers depending on which is the first consultation, for example, first user message: exit ('user_y') exit ('user_n') and email: exit ('email_y') exit ('email_n' )

and at the "success" of the plugin you are using a switch as follows:

success: function(data){ 

      switch(data)
      {
        case 'user_y':
              alert('user_msg_yes');
        break;
        case 'user_n':
              alert('user_msg_no');
        break;
        case 'email_y':
              alert('email_msg_yes');
        break;
        case 'email_n':
              alert('email_msg_no');
        break;
      }
}

Upvotes: 1

Related Questions