Welcome Guest, Not a member yet? Register   Sign In
Array of Validation Errors -- With Field Names!
#1

[eluser]CodeIgniterNewbie[/eluser]
Is there a way to get an array of validation errors in $key => $value format, where $key is the name of the field and $value is the actual validation error.

I could explode validation_errors() using "\n" as the delimiter, but that only gives me the error messages. I need this information paired with its associated field. I want to be able to do this without have explicit knowledge of the form that is being validated.

By the way, this produces an extra element at the end. It contains an empty string. Does anyone know why?

Code:
var_dump(explode("\n", validation_errors()));
#2

[eluser]CodeIgniterNewbie[/eluser]
I figured it out:

Code:
$errors = $this->form_validation->_error_array;

foreach ($errors as $key => $value)
{
    // Do whatever here
}
#3

[eluser]SitesByJoe[/eluser]
Nice one!
#4

[eluser]Dyllon[/eluser]
While this works it's also somewhat wrong, _error_array is a private property.
You should extend the form_validation class with a new method to access the property.
#5

[eluser]CodeIgniterNewbie[/eluser]
What made you say it was private? You do see that I was able to access it directly, right?
#6

[eluser]Dyllon[/eluser]
Codeigniter is PHP 4 compatible and as such does not take advantage of PHP 5 "Visibility", instead they use naming conventions as described in the Style Guide
#7

[eluser]CodeIgniterNewbie[/eluser]
I see.




Theme © iAndrew 2016 - Forum software by © MyBB