Welcome Guest, Not a member yet? Register   Sign In
Custom Validation Messages not shown
#1

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
Reply
#2

(This post was last modified: 08-02-2024, 04:57 PM by kenjis.)

See https://codeigniter.com/user_guide/incom...s-validate

But as the Important note says, we recommend that you use the validateData() method instead.
https://codeigniter.com/user_guide/incom...lidatedata
Reply
#3

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());
        
Reply
#4

(08-02-2024, 04:51 PM)kenjis Wrote: See https://codeigniter.com/user_guide/incom...s-validate

But as the Important note says, we recommend that you use the validateData() method instead.
https://codeigniter.com/user_guide/incom...lidatedata

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.
Reply
#5

Perfect !
Thanks you Smile
Reply




Theme © iAndrew 2016 - Forum software by © MyBB