Description
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
- Define a custom validation rule (e.g., valid_enum) in your validation rules.
- Inside the rule, try to return a custom error message with the {field} placeholder.
- 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.