Welcome Guest, Not a member yet? Register   Sign In
Edit Profile Validation
#1

[eluser]Kemik[/eluser]
Hello all,

I'm coding an edit profile page. I've pulled the user's info in to the form fields and everything is working great.

However, the validation for both of the user's fields (username and email) require that they must be unique.

How can I best implement the following

Code:
if ($str != user's current username/email) {

  $this->db->where('username', $str);
  $this->db->or_where('email', $str);
  $this->db->from('users');
        
  $query = $this->db->get();
        
  return $query->num_rows();

} else {

  return 0;

}

The above code goes in the model used in the callback.

Callback:
Code:
function unique($str)
    {
        $found = $this->user_model->unique_check($str);
        
        if ($found > 0) {
            // Found email or username = Not available
            $this->validation->set_message('unique', 'The %s you selected is currently in use. Please choose another.');
            return FALSE;
        } else {
            return TRUE;
        }
    }

Basically, the code will return 1 (or more) if the username or email matches those in the database. I want it to ignore the user's current details though. This is to combat the validation error returned when I only want to edit the user's username and not the email (and visa versa).

The problem I'm having is getting the user's current email/username to the model. I think only $str can be passed to callback function.
#2

[eluser]tonanbarbarian[/eluser]
you are better off using 2 callbacks, one for each field.

from there you have a couple of options

1. the first call back can store its data as a property of the controller and the second callback can grab the first data and its own and process

2. each callback only validates it own data. you should be able to right a model method that can be used by either to valid to reuse code tho
you might even have each of the callbacks call a third private method that takes the value and a flag indicating which type to check to even further reuse code

if none of that is really what you want then just use the 1 callback and put it only on the 1 field, not both
then in the callback use the input->post to retrieve the second value and test it as well

there may be even more options but i cannot think of them at this time




Theme © iAndrew 2016 - Forum software by © MyBB