1

Am using codeigniter version (2.1.4). Am asking if the validation library provides a solution to access validation errors as an array instead of validtation_errors() method.

Regards

6
  • Nope If you need to display the error message somewhere. Use variable to start the html section and use it... Commented Oct 5, 2013 at 10:53
  • @Nes Your answer is not clear. Please clarify the picture for me ! Commented Oct 5, 2013 at 10:56
  • There will not return you a array in codeigniter. What purpose you need it as array? Commented Oct 5, 2013 at 10:57
  • @Nes for some reason I want to loop through the produced error messages. Commented Oct 5, 2013 at 11:01
  • You have option to change the error messages in codeigniter. Commented Oct 5, 2013 at 11:03

3 Answers 3

2

You need to extend the form validation library CI_Form_validation define your function validation_error_array variable $this->_error_array will have the all the errors

class MY_Form_validation extends CI_Form_validation
{
    function validation_error_array()
    {
        return $this->_error_array;
    }
}

See for reference

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

3 Comments

Ok , the inheritance makes since. But PHP still not supporting multiple inheritance since am extending CI_CONTROLLER as well !
While when you load form validation library instead load your validation library MY_Form_validation and when you call validation_error_array() using your library you will have the errors array donot extend your controller to validation library just load your new validation library
Good tip , lets me do that ! If it is worked I will back to you to approve your answer. Thanks for help !
2

I don't know if it works in your specified version but in the latest version you can use

$this->form_validation->error_array()

and pass that to view or even better post your form as ajax request and get error messages as array back. You don't need to initialize form helper unless you are using other form elements

Comments

1

You can use the validation_errors() to show all the errors in one place or you can show each error message with respective field in the following way.

 <?php echo form_error('username'); ?>
 <input type="text" name="username" value="<?php echo set_value('username'); ?>" size="50" />

and also you can change the error delimiters.

Global delimiters.

$this->form_validation->set_error_delimiters('<div class="error">', '</div>');

Individual delimiters.

<?php echo form_error('field name', '<div class="error">', '</div>'); ?>

Or:

<?php echo validation_errors('<div class="error">', '</div>'); ?>

So i dont think you should have a need to get validation errors in an array.

3 Comments

Actullay I like your answer. But the need for the array still needed. I looked to some validation engines and they provide a way to access the errors as an array. This problem is one of the drawbacks of Form Validation in Codeigniter !
i dont think it as a drawback as its providing both the ways to show the error messages.
I respect your opinion. But as you know some people wanna to process those messages in some way by looping through the elements. As a developer I think it is very important to provide such facility !

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.