Welcome Guest, Not a member yet? Register   Sign In
Could Use Some Fresh Eyes - Form Validation Problem
#1

[eluser]Jay Logan[/eluser]
I just can't seem to wrap my head around why the following code will not display form errors. I believe that the form processing is checking the rules though because it will not log a user in if the fields are blank. It just reloads the page. Does anyone see any noticeable errors with the code below?

Controller:
Code:
function in()
    {
        $this->load->library('form_validation');
        
        $this->form_validation->set_rules('user_name', 'User Name', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required');
        
        if ($this->form_validation->run() == FALSE) {
        
            $this->layout->view('manage/log_in');
            
        } else {
        
            $this->load->model('auth_model');
            $user_id = $this->auth_model->check_auth($this->input->post('user_name'), $this->input->post('password'));

            if ($user_id) {
            
                $this->session->set_userdata('user_id', $user_id);
                redirect('manage');
            
            } else {
                
                redirect('manage/log/in');
            
            }
        
        }
        
    }

View

Code:
<?php if (validation_errors() == TRUE): ?>

    <div class="login_message message error">
        &lt;?= validation_errors() ?&gt;
    </div>
    
&lt;?php endif; ?&gt;
#2

[eluser]stef25[/eluser]
If you try to print out the validation errors right after this line, what do you get?

Code:
if ($this->form_validation->run() == FALSE) {
#3

[eluser]Jay Logan[/eluser]
Nothing is shown. It's like the error messages either aren't being set or aren't being displayed. I'm deleting everything and installing a fresh CI install then re-building my app piece-by-piece until I figure this out. I will not lose...Lol.
#4

[eluser]nevsie[/eluser]
can i see your full view file?
just one idea is that you are actually passing something to the form even if it is a " " or NULL value, therefore, it is getting values even if it is nothing, and trying to run the login - but as the login fails it takes that redirect path instead.

Although just seeing a typo in you script means that this is unlikely as the redirect on failed login has a / instead of a _ and so this would probably throw a 404 or similar!
#5

[eluser]danmontgomery[/eluser]
Code:
&lt;?php if (validation_errors() == TRUE): ?&gt;

validation_errors() doesn't return a boolean, it returns a string

Code:
&lt;?php if (strlen(validation_errors()) > 0): ?&gt;
#6

[eluser]Jay Logan[/eluser]
So I have it working now with the above code. I deleted everything, installed latest CI, then added the controller and view. Works perfectly. I guess I maybe loaded something in the previous setup that was interrupting form_validation. At least I'm not wrecking my brain anymore.

Thanks to all for the help.




Theme © iAndrew 2016 - Forum software by © MyBB