CodeIgniter Forums
Custom Validation Messages not shown - 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: Custom Validation Messages not shown (/showthread.php?tid=91387)



Custom Validation Messages not shown - dipi66 - 08-02-2024

Hello everyone,

I'm trying to validate a form with a custom error message.

The message displayed is always the default, in English. Here is my code:

PHP Code:
        $validation = \Config\Services::validation();

        $validation->setRules([
 
'sonde_nom' => [
                'label' => 'Nom',
                'rules' => 'required|min_length[3]',
                'errors' => [
                    'required' => 'Le champ {field} est obligatoire.',
                    'min_length' => 'Le champ {field} doit contenir au moins {param} caractères.',
                ],
            ],
 
 ]);
        if (!$this->validate($validation->getRules())) {
            return redirect()->back()->withInput()->with('errors'$validation->getErrors());
        }
 
print_r($validation->getRules());die(); 

For testing I print the getRules():
PHP Code:
Array ( [sonde_nom] => Array ( [label] => Nom [rules] => Array ( [0] => required [1] => min_length[3] ) ) ) 

What did I miss? Thank you in advance for your help Smile


RE: Custom Validation Messages not shown - kenjis - 08-02-2024

See https://codeigniter.com/user_guide/incoming/controllers.html#this-validate

But as the Important note says, we recommend that you use the validateData() method instead.
https://codeigniter.com/user_guide/incoming/controllers.html#this-validatedata


RE: Custom Validation Messages not shown - kenjis - 08-02-2024

Try:

PHP Code:
        $rules = [
            'sonde_nom' => [
                'label' => 'Nom',
                'rules' => 'required|min_length[3]',
                'errors' => [
                    'required' => 'Le champ {field} est obligatoire.',
                    'min_length' => 'Le champ {field} doit contenir au moins {param} caractères.',
                ],
            ],
        ];

        if (! $this->validate($rules)) {
            // The validation failed.
            dd($this->validator->getErrors());
        



RE: Custom Validation Messages not shown - Bosborne - 08-03-2024

(08-02-2024, 04:51 PM)kenjis Wrote: See https://codeigniter.com/user_guide/incoming/controllers.html#this-validate

But as the Important note says, we recommend that you use the validateData() method instead.
https://codeigniter.com/user_guide/incoming/controllers.html#this-validatedata

I see that was introduces way back in 4.2.0, before I started with CI.  I somehow missed that. Now I need to update my projects, most of which are nearing deployment.


RE: Custom Validation Messages not shown - dipi66 - 08-04-2024

Perfect !
Thanks you Smile