Welcome Guest, Not a member yet? Register   Sign In
display login error
#1

[eluser]runrun[/eluser]
Hi,

I know that we can use the callback function in form validation to display additional customed rule.

However, I am using method displaying error individually. Thus errors like "either username or password is incorrect" or "your account is not activated" aren't fit any fields next to them.

So how could I display those type of error message on top of the form?
#2

[eluser]pistolPete[/eluser]
What callbacks/rules do you use? Post your code please.
#3

[eluser]runrun[/eluser]
yes, here.

Code:
class Login extends Controller
{
    function index()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<span class="error">', '</span>');

        $config = array(
                    array(
                          'field'   => 'email',
                          'label'   => 'Email',
                          'rules'   => 'required|trim|valid_email'
                        ),
                    array(
                         'field'   => 'password',
                         'label'   => 'Password',
                         'rules'   => 'required|trim'
                        )
                    );
        $this->form_validation->set_rules($config);
        
        if($this->form_validation->run()==FALSE)
        {
            $this->load->view('login_view');
        }
        else
        {
            $query = $this->db->query("SELECT
                                            , username
                                            , activation
                                         FROM user
                                        WHERE email='".$this->input->post('email')."'
                                            AND password = '".md5($this->input->post('password'))."' "
                                      );
            if($query->num_rows()>0)
            {
                $row = $query->row();
                if($row->activation == 1)
                {
                    set_cookie('username',$row->username,7200);
                    redirect('account');
                }
                else
                {
                    'account is not yet activated';
                }
            }
            else
            {
                'either username or password is incorrect';
            }
        }
    }
}
I want to pass the error messages 'acount not yet..' and 'either username ..' to the view but don't know how
#4

[eluser]pistolPete[/eluser]
I would use one callback for both fields:

Code:
&lt;?php

class Login extends Controller
{
    function index()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<span class="error">', '</span>');

        // added the callback rule
        $config = array(
                    array(
                          'field'    => 'email',
                            'label'    => 'Email',
                          'rules'    => 'trim|required|valid_email'
                        ),
                    array(
                         'field'   => 'password',
                         'label'   => 'Password',
                         'rules'   => 'trim|required|callback__check_login'
                        )
                    );
        $this->form_validation->set_rules($config);
        
        if($this->form_validation->run() === FALSE)
        {
            $this->load->view('login_view');
        }
        else
        {
            redirect('account');
        }
    }
    
    function _check_login($password)
    {
        $email = $this->input->post('email');
        
        $query = $this->db->query('SELECT username, activation FROM user WHERE email="'.$email.'" AND password = "'.md5($password).'"');
        
        if($query->num_rows() == 1)
        {
            $row = $query->row();
            if($row->activation == 1)
            {
                // maybe use sessions class ?
                set_cookie('username',$row->username,7200);
                return TRUE;
            }
            else
            {
                $this->form_validation->set_message('_check_login', 'account is not yet activated');
                return FALSE;
            }
        }
        else
        {
            $this->form_validation->set_message('_check_login', 'either username or password is incorrect');
            return FALSE;
        }
    }
}

/* End of file login.php */
/* Location: ./system/application/controllers/login.php */

I didn't test the code but it should work.

Btw: I suggest having a look at the session library instead of using "raw" cookies.
#5

[eluser]runrun[/eluser]
If we have a form, where we display validation message inline with form fields, certainly general errors like "account not yet activated" should be display on top/bottom of the form, therefore I would set my form look like this

The problem is we will have double error messages. Because function validation_errors and form_error() will do the same thing at the same time.
Code:
&lt;?=validation_errors()?&gt;
&lt;form id="login_form" method="post"&gt;
    <div>
        <label for="email">Email</label>
        &lt;input type="text" name="email" id="email" value="&lt;?=set_value('email')?&gt;" /&gt;
        &lt;?=form_error('email')?&gt;
    <div>
    <div>
        <label for="password">Password</label>
        &lt;input type="password" name="password" id="password" /&gt;
        &lt;?=form_error('password')?&gt;
    </div>
        &lt;input type="submit" value="Login" name="login" /&gt;
&lt;/form&gt;
#6

[eluser]runrun[/eluser]
well, thank you, that won't solve the desire but still help a alot.




Theme © iAndrew 2016 - Forum software by © MyBB