Search code examples
phpvalidationcodeigniter-4

Correct syntax for CodeIgniter 4 Validation matches option


I am implementing a registration section for a site, using CodeIgniter 4.0.4.

I implemented the following rules in my Validation configuration file.

public $registro = [
    'registro.nombre' => [
        'rules' => 'required',
        'errors' => [
            'required' => 'El nombre es un campo obligatorio.'
        ]
    ],
    'registro.apellido' => [
        'rules' => 'required',
        'errors' => [
            'required' => 'El apellido es un campo obligatorio.'
        ]
    ],
    'registro.email' => [
        'rules' => 'required|valid_email',
        'errors' => [
            'required' => 'El correo electrónico es un campo obligatorio.',
            'valid_email' => 'Por favor, revisa el formato de tu correo electrónico, no parece ser válido.'
        ]
    ],
    'registro.password' => [
        'rules' => 'required|min_length[6]',
        'errors' => [
            'required' => 'La contraseña es un campo obligatorio.',
            'min_length' => 'La contraseña debe ser de al menos 6 caracteres.'
        ]
    ],
    'registro.verificar_password' => [
        'rules' => 'required|matches[registro.password]|min_length[6]',
        'errors' => [
            'required' => 'Verificar contraseña es un campo obligatorio.',
            'matches[registro.password]' => 'Las contraseñas no coinciden, por favor revisa.',
            'min_length' => 'Verificar contraseña es un campo que debe ser de al menos 6 caracteres.'
        ]
    ]
];

Everything works fine, except the matches[registro.password] option. The problem is that I am not sure how to add it in the errors message. Inside the documentation there isn't a specific example for this.

My inputs (reduced code) for the passwords are these:

<input type="password" name="registro[password]" minlength="6" id="registro_password" placeholder="Contraseña (mínimo 6 caracteres)" required />
<input type="password" name="registro[verificar_password]" minlength="6" data-equalto="registro_password" id="registro_verificar_password" placeholder="Verificar contraseña" required />

So, basically when I send the info to my controller I get an array with those two indexes. I make sure that the passwords are the same, but I keep getting that error. I tried to change the errors index for matches to these two versions:

// Version 1
'matches[registro.password]' => 'Las contraseñas no coinciden, por favor revisa.'
// Output
// The registro.verificar_password field does not match the registro.password field.
// This is not a message I have defined as you can see above

// Version 2
'matches' => 'Las contraseñas no coinciden, por favor revisa.'
// Output
// Las contraseñas no coinciden, por favor revisa.
// This is the message I have defined above.

For this example I am literally using 123456 as a password.

I tried doing this and here are the results:

var_dump($post['registro']['password'] == $post['registro']['verificar_password']);
// Output
// bool(true)

if(!$validation->run($post, 'registro')) {
    echo $validation->listErrors();
}
// Output 
// Las contraseñas no coinciden, por favor revisa.

Am I doing something incorrectly, or is this a validation library bug?


Solution

  • Ok, I checked this with the CodeIgniter 4 team, creating an issue report.

    This was indeed a bug, which by when people read this would be fixed. Just to clear up my own question, the correct way of using the errors message for the matches index would be:

    'registro.verificar_password' => [
        'rules' => 'required|matches[registro.password]|min_length[6]',
        'errors' => [
            'required' => 'Verificar contraseña es un campo obligatorio.',
            'matches' => 'Las contraseñas no coinciden, por favor revisa.',
            'min_length' => 'Verificar contraseña es un campo que debe ser de al menos 6 caracteres.'
        ]
    ]
    

    Basically the index should be only matches in the errors array.