4

I'm attempting to use CodeIgniters form validation library, and despite my validation rules appearing to work, whenever I call validation_errors(), I get an empty string.

Here's a code snippet.

$base_rules = 'required|trim';

$this->_validation_rules = array(
        array(
            'field' => 'name',
            'label' => 'name',
            'rules' => $base_rules . '|alpha_numeric|min_length[5]|max_length[30]'
        ),
        array(
            'field' => 'price',
            'label' => 'Price',
            'rules' => $base_rules . '|decimal'
        ),
        array(
            'field' => 'duration',
            'label' => 'Duration',
            'rules' => $base_rules . '|integer'
        ),
        array(
            'field' => 'booking',
            'label' => 'Booking',
            'rules' => $base_rules . '|integer'
        )
    );
    $this->form_validation->set_rules($this->_validation_rules);

    if ($this->form_validation->run()) {
        // do stuff
    }else{
        // prints an empty string
         var_dump(validation_errors());
         exit;
    }

Does anyone know why this is the case, and how I can get my errors?

3 Answers 3

5

Put validation_errors() on top of your form, and change your code to

 if ($this->form_validation->run()) {
        // do stuff
    }else{
       $this->load->view('myform'); //display your form again
    }

validation_errors() will only by poplated in a view.

Sign up to request clarification or add additional context in comments.

2 Comments

Great thanks, one question though - does this mean there is no way of storing the validation errors in flashdata or some other storage so I can redirect somewhere else? Cheers
@John what if we're sending an AJAX request? of-course, then we need to get the errors in Controller rather than View. So, is there any solution to get errors in Controller? validation_errors() is not working in Controller.
1

put this code top of form in view file

echo validation_errors('<div>', '</div>');

then change else to this

else{
    $this->load->view('myform'); //display your form again
}

Comments

1

To complete John answer, if you did a form post you can put under your inputs :

<?php echo form_error('input-name'); ?>

But if you're making an ajax request(wich is not your case now but in the future if you need) you can use the validation_errors().

Here an exemple :

    //Important to turn that off if it's on
    $this->output->enable_profiler(false); 

    $this->output->set_status_header('500');
    $this->output->set_content_type('application/json');

    echo json_encode(array(
        'error_msg' => validation_errors(),
    ));

And then on your client-side you can use the response like that :

error:function(data) {
    $("your-error-input-selector").html('').append(data.responseJSON.msg);
}

Hope i helped even if i'm 1 year late.

P.S Sorry for my broken english.

1 Comment

Did you add your form validation ? Did you run your validation ? can i take a look at your code ? I'll be more than happy to help you !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.