CodeIgniter Forums
How to display form validation error on view page? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=31)
+--- Thread: How to display form validation error on view page? (/showthread.php?tid=71995)



How to display form validation error on view page? - dipesh - 10-23-2018

Hello friends!

In Codeigniter3 we can use helper function form_error() to display single error.
e.g: echo form_error('username');   // systax of CI3.

But in codeigniter4 we have to write like this:

// create object of validation.
$validation =  \Config\Services::validation();
echo $[b]validation->getError('username');[/b]

So its very tedious task (in CI4) to create object in every view page and then display single error.

Is there any helper function likes CI3 that we can use in CI4 ?


RE: How to display form validation error on view page? - InsiteFX - 10-24-2018

PHP Code:
// list all errors
<?= \Config\Services::validation()->listErrors(); ?>

// list defined error
$error = $validation->getError('username'); 

You also create custom error view files see the User Guide on validation library.