Welcome Guest, Not a member yet? Register   Sign In
Edit validation problem
#1

[eluser]jiahui[/eluser]
I am working on mine Edit application.

For example :

As codeigniter tutorial described, if want check whether is username available to use we can set a callback_username_check function at set_rules query there and then set username_check function after that.

My question is, how can I pass $userId in function username_check? Please help.

Code:
// --------------------------------------------------------------------

    function _validation()
    {
        $rules['firstname']     = 'trim|required|min_length[2]|max_length[50]|htmlspecialchars';
        $rules['lastname']         = 'trim|required|min_length[2]|max_length[50]|htmlspecialchars';
        $rules['email']         = 'trim|required|htmlspecialchars|valid_email|max_length[100]';
        $rules['username']         = 'trim|required|htmlspecialchars|max_length[100]|callback_username_check';
        $rules['password']             = 'trim|required|min_length[6]|matches[password_confirm]';
        $rules['password_confirm']         = 'trim|required|htmlspecialchars|min_length[6]|';
        $this->validation->set_rules($rules);

        $fields['firstname']     = $this->lang->line('user_first_name');
        $fields['lastname']         = $this->lang->line('user_last_name');
        $fields['email']     = $this->lang->line('user_email');
        $fields['username']     = $this->lang->line('user_username');
        $fields['password']         = $this->lang->line('user_password');
        $fields['password_confirm']     = $this->lang->line('user_password');
        $this->validation->set_fields($fields);

        $this->validation->set_error_delimiters('<span class="error">', '</span>');
    }
    
// --------------------------------------------------------------------
    function username_check($str)
    {
    
        $SQL="select * from user where username='".$str."' and id <> '".$userId."'";
        $rs = mysql_query($SQL) or die(mysql_error());
        if(mysql_num_rows($rs) > 0)
        {        
            $this->validation->set_message('username_check', $str.' is already in use');
            
            return FALSE;
            
        }else{
        
            return TRUE;
        }
    
    }
#2

[eluser]rogierb[/eluser]
Havnt tried this but looking at the form_validation library something like this:
Code:
callback_username_check['.$userid.']

function username_check($str,$val)

where $val = $userid
#3

[eluser]Flemming[/eluser]
you can also access any posted data inside your callback function:

Code:
function username_check()
{
    if($this->input->post('username')) ...
}
#4

[eluser]jiahui[/eluser]
Ya. Flemming, you are right!! Thank you so much.




Theme © iAndrew 2016 - Forum software by © MyBB