CodeIgniter Forums
form_validation return after first field error ? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11)
+--- Thread: form_validation return after first field error ? (/showthread.php?tid=72742)



form_validation return after first field error ? - Ernestas - 02-05-2019

Hi, so recently i started to use CI, and im at a point where i need to validate my forms and i have a question

Is there a good reason for this code ?
Code:
if ( ! isset($this->_error_array[$row['field']]))
{
    $this->_error_array[$row['field']] = $message;
}

return;

(Line 814 @ system/form_validation).

I don't want to piss off visitors with just one error of specific field at a time, i want to show then all.

Quote:I want to get this:
 ["xxx"]=>
 array(2) {
   [0]=>
   string(72) "The xxx field must be at least 3 characters in length."
   [1]=>
   string(72) "The xxx field does not match the yyy field."
 }

Not this:
 ["xxx"]=>
 array(1) {
   [0]=>
   string(72) "The xxx field must be at least 3 characters in length."
 }

So from the code above i'm now using only this
Code:
$this->_error_array[$row['field']][] = $message;

i don't like to mess with core code, so is this the right approach ? maybe im missing something ?

--
sorry for English Smile


RE: form_validation return after first field error ? - php_rocs - 02-05-2019

@Ernestas,

Are you using the builtin CI form validation ( https://codeigniter.com/user_guide/libraries/form_validation.html?highlight=validation#setting-validation-rules )?


RE: form_validation return after first field error ? - Ernestas - 02-06-2019

@php_rocs, yes


RE: form_validation return after first field error ? - ciadmin - 02-16-2019

If your question is actually whether you can get all errors instead of just the first one, then yes ... https://www.codeigniter.com/user_guide/libraries/form_validation.html#CI_Form_validation::error_array

Treat that as an array, and iterate over it, building your responses or view parameters ... possible.
Or, the user guide (same page) shows splitting them off individually, to show specific error message beside each field (more work).