CodeIgniter4 icon indicating copy to clipboard operation
CodeIgniter4 copied to clipboard

Custom Validation Rule Error: {field}, {param} , {value} Placeholders Not Replaced in Error Messages

Open maniaba opened this issue 1 year ago • 4 comments

PHP Version

8.1, 8.2

CodeIgniter4 Version

4.5.5

CodeIgniter4 Installation Method

Composer (using codeigniter4/appstarter)

Which operating systems have you tested for this bug?

Windows, Linux

Which server did you use?

apache

Database

No response

What happened?

When creating a custom validation rule, the custom error message returned from the rule does not allow for including specific data about the field being validated. Even if we include the {field} placeholder in the message, it does not get replaced with the actual field name, which limits the usefulness of the error message.

Steps to Reproduce

  1. Define a custom validation rule (e.g., valid_enum) in your validation rules.
  2. Inside the rule, try to return a custom error message with the {field} placeholder.
  3. Perform validation using this rule and check the returned error message.
$validation->setRules([
    'status' => [
        'label' => 'Status',
        'rules' => 'valid_enum[active,inactive]',
    ],
    'user' => [
        'label' => 'User',
        'rules' => 'valid_enum[active,inactive]',
    ],
]);

// Custom validation rule
function valid_enum($value, $params, $data = null, &$error = null): bool
{
    $validValues = explode(',', $params);
    if (!in_array($value, $validValues)) {
        $error = 'The field {field} must be one of: ' . implode(', ', $validValues);
        return false;
    }
    return true;
}

Expected Output

The expected behavior is that the {field} placeholder should be replaced with the actual field name being validated, in this case, status. For example, the error message should be:

The field Status must be one of the valid values.

However, the {field} placeholder does not get replaced, and the error message is returned without the field name, reducing the clarity of the validation error.

Anything else?

This limitation affects all custom rules where dynamic placeholders such as {field}, {param} , {value} are expected to be replaced by the validation system. Without this, the error messages lack context, making it difficult to identify which field caused the validation failure.

maniaba avatar Oct 09 '24 09:10 maniaba

See PR #9201

neznaika0 avatar Oct 09 '24 11:10 neznaika0

See PR #9201

This isn't the same issue, as I am referring here to updating the &$error parameter directly. Validation::getErrorMessage will never run when we set $error. However, I did notice that we now have a new $field parameter in the method as the fifth input parameter, which I can use to solve this.

The only limitation is that if the validation rule doesn’t include $params, then $field won't be passed into the function. https://github.com/codeigniter4/CodeIgniter4/blob/118c2c48ab551e180a4472caaaf51699604020ec/system/Validation/Validation.php#L338

maniaba avatar Oct 09 '24 15:10 maniaba

Just use language translations:

// app/Language/en/Validation.php
return [
    'example' => 'The {field} field name is here.'
];
// app/Validation/ExtraRules.php
public function example($str)
{
    if (empty($str)) {
        return false;
    }

    return true;
}

The other option is to declare an empty params, like:

$rules = [
    'field' => ['label' => 'My example', 'rules' => 'example[]'],
];

Then you can use the extended version of parameters in your rule.

michalsn avatar Oct 11 '24 07:10 michalsn

Just use language translations:

// app/Language/en/Validation.php
return [
    'example' => 'The {field} field name is here.'
];
// app/Validation/ExtraRules.php
public function example($str)
{
    if (empty($str)) {
        return false;
    }

    return true;
}

The other option is to declare an empty params, like:

$rules = [
    'field' => ['label' => 'My example', 'rules' => 'example[]'],
];

Then you can use the extended version of parameters in your rule.

I believe it would be beneficial for the $error variable to also pass through the getErrorMessage method. This would allow custom validation rules to take advantage of the same dynamic placeholder replacement system used by built-in rules, ensuring that placeholders like {field}, {param}, and {value} are properly replaced in custom error messages.

https://github.com/codeigniter4/CodeIgniter4/blob/118c2c48ab551e180a4472caaaf51699604020ec/system/Validation/Validation.php#L366

maniaba avatar Oct 13 '24 04:10 maniaba