form_error method in Codeigniter4 ?

Codeigniter3 has form_error method that is used to display the validation error messages.
Codeigniter handles this scenario in a different way. The form_error method does not exist in CI4,
but there is a validation object that contains methods like getError(), showError() that does the job.

You can refer the validation example here. http://dvxlab.com/index.php/codeigniter4/form-validations-in-codeigniter4-with-examples/

We can see that, to display a single error message we use the $validation->getError() method in CI4 in place of form_error in CI3.
getError() method take a string parameter that has the field input name.

We can read all the errors, using the method getErrors() with no parameter.

We can also write custom rules and write our own error messages as required in the setRule method.

$this->validation->setRule('email', 'Email',['required','valid_email'],['valid_email'=>'User Email is invalid.']);

See how to write custom rules to validate data from database here http://dvxlab.com/index.php/codeigniter4/create-custom-validation-rule-in-codeigniter4/

We can also customize the way our error text is shown with a custom template for validation errors as well. See how to write a customized error message here. http://dvxlab.com/index.php/codeigniter4/how-to-write-custom-template-for-validation-error-messages-in-codeigniter4/

Leave a Reply