Welcome Guest, Not a member yet? Register   Sign In
Best way to pass error messages.
#1

[eluser]silverback[/eluser]
Hi,

What is the best way to pass error statuses?
For example, I have a user controller which has a login method, and a validate_login method:

Code:
function login($error = 0)
    {
        $data = array(
            'title' => 'Login',
            'main_content' => 'login_view',
            'error' => $error
        );
        $this->load->view('includes/template', $data);
    }
    
    function validate_login()
    {
        $error = $this->user_model->validate_login();
        if ($error < 0)
        {
            $this->login($error);
        } else
        {
            redirect('somepage');
        }
    }

user_model->validate_login() checks that the form data is correct with form validator, and then checks the username/password against the DB (returns -1 and -2 respectively). If I were just checking the form data, there would be no need to pass error statuses, but I want to display a message if the login details are incorrect.

While the above works fine, it doesn't seem particularly elegant and I wanted to ask if there is a standard way of doing this?

Thanks
#2

[eluser]richzilla[/eluser]
You can use the session flashdata element. If the login fails, simply set the flashdata variable:

Code:
$this->session->set_flashdata('status','You could not be logged in');

This is then available on the next page view, so you would simply redirect to your login page:

Code:
redirect('login');

and then in your login view, have something similar to:

Code:
if($this->session->flashdata('status')): echo $this->session->flashdata('status'); endif; ?&gt;

The message will be displayed. The flashdata variable is automatically destroyed on the next page request.
#3

[eluser]silverback[/eluser]
Perfect thanks, that exactly what I need.

Edit: I just noticed a problem. When I use redirect, validation_errors() doesn't work any more. Any way to fix this?

Another question:
is using redirect() considered good practice?
#4

[eluser]silverback[/eluser]
bump
#5

[eluser]kirkaracha[/eluser]
Redirecting to a confirmation page, etc. is good practice once the form is valid because it helps prevent the form from being submitted twice if they reload the page. I usually have a do the form validation in the same method so you submit the form to itself, then redirect if it's valid.

Controller:

Code:
function login() {
    $this->load->library('form_validation');
    $this->load->model('Login_model');

    $config = array(
        array(
            'field' => 'username',
            'label' => 'username',
            'rules' => 'trim|required'
        ),
        array(
            'field' => 'password',
            'label' => 'password',
            'rules' => 'trim|required|callback__check_login'
        )
    );
    $this->form_validation->set_rules($config);

    if ($this->form_validation->run() == FALSE) {
        // display the login form
        // without errors at first and with errors if there are any once the form is submitted
    } else {
        // if there aren't any errors, set a session variable saying they're logged in
         $session_data['logged_in'] = TRUE;
        // redirect someplace else
        redirect('/menu/','location');
    }
} // login

function _check_login(){
    // set variables to submitted form data
    $username = $this->input->post('username');
    $password = md5($this->input->post('password'));

    $check_password = $this->Login_model->check_login($username,$password);
    if ($check_password->num_rows() == 1){
        return TRUE;
    } else {
        $this->form_validation->set_message('_check_login',"The username and password don't match.");
        return FALSE;
    }

View:

Code:
&lt;?php if(!empty($this->form_validation->_error_array)):?&gt;
<div id="errors">

<p>Sorry, we had some trouble with your form.</p>

<ul>
&lt;?php foreach($this->form_validation->_error_array as $error): ?&gt;
    <li>&lt;?php echo $error; ?&gt;</li>
&lt;?php endforeach; ?&gt;
</ul>

</div>&lt;!-- /errors --&gt;

&lt;?php endif; ?&gt;

// login form

Flashdata is also useful. I use it when making edits: I set a flashdata saying an item was updated and redirect to the item's detail page and show an "item updated" message on the page if there's a flashdata.
#6

[eluser]silverback[/eluser]
Thanks kirkaracha

Unfortunately, my form validation is in my model which apparently precludes me from using callbacks. I had to revert to my original method of passing error codes.




Theme © iAndrew 2016 - Forum software by © MyBB