The use of the $message
parameter is mentioned in validate to define the error message for validation,
message variable
The error message $message
parameter in Validator as well as in Rule Object supports the use of:
:attribute
Name of the field currently being validated:input
Current validation value:index
Index of the currently validated array{:field}
Specify the value of the field{@field}
Specify the name of the field{&index}
Get the index of the array in the trigger error{&&index}
Get the location of the array in the trigger error
Tips
The above text is supported in custom rules using
:attribute
:attribute
represents the name of the field in the customAttributes
variable that currently triggers the error:
class Test extends Validate
{
protected $rule = [
'user' => 'required'
];
protected $message = [
'user.required' => 'Please fill in the :attribute'
];
protected $customAttributes = [
'user' => 'account'
];
}
After triggering, the message prompted is Please fill in the account
{:field}
The field
in the middle of {:field}
is the field of the current validation value. If the specified field does not exist, it is empty text, support for getting an array, such as info.name
, on behalf of getting the name
parameter in the info
array. supports unlimited depth.
class Test extends Validate
{
protected $rule = [
'name' => 'chs'
];
protected $message = [
'name.chs' => 'The name you filled in {:name} is not a Chinese name'
];
}
Enter data ['name' => 'Rasmus Lerdorf']
,prompt:The name you filled in Rasmus Lerdorf is not a Chinese name
Array
If the error is triggered by a value in an array, you can also get it directly: {:data.*.user.name}
{@field}
{@field}
means use the custom name of field
class User extends Validate
{
protected $rule = [
'pass' => 'required',
'repass' => 'required|same:pass'
];
protected $customAttributes = [
'pass' => 'Password',
'repass' => 'Confirm Password'
];
protected $message = [
'repass.same' => ':attribute and {@pass} do not match'
];
}
The last error message triggered is:Confirm Password and Password do not match
{&index} And {&&index}
If a multidimensional array triggers an error in validation, we need to use the index in the error message, or we need a field value in the array.
index
is the nth index in the array
class Test extends Validate
{
protected $rule = [
'button' => 'required|array:@list|max:3',
'button.*' => 'required|array:name,sub_button',
'button.*.name' => 'required|alpha_dash',
'button.*.sub_button' => 'required|array:@list',
'button.*.sub_button.*' => 'required|array:name,type',
'button.*.sub_button.*.name' => 'required|alpha_dash',
'button.*.sub_button.*.type' => 'required|numeric',
];
protected $message = [
'button.*.sub_button.*.type.required' => 'Missing type of {&&1} submenu of {:button.*.name} with index {&1}'
];
}
Test::make()->check([
'button' => [
[
'name' => 'Main-Menu-1',
'sub_button' => [
[
'name' => 'Sub-menu-1-1',
'type' => 1
],
[
'name' => 'Sub-menu-1-1',
'type' => null
]
]
]
]
]);
will output the error Missing type of 2 submenu of Main-Menu-1 with index 1
Joint use
You can also {:field}
together to get the desired value, e.g. :{:button.{&0}.sub_button.{&1}.name}
customAttributes variable
When we define many validation fields and rules, it would take a lot of time and cost to write error messages one by one correspondingly. At this time, we can use the $customAttributes
variable to define the names of the fields.
Automatically replaces the :attribute
text in the default error message when the error is triggered
class User extends Validate
{
protected $rule = [
'id' => 'required|numeric',
];
protected $customAttributes = [
'id' => 'User ID',
];
}
When the error is triggered, it will prompt User ID is required
{:field}
is also supported in the customAttributes variable, e.g:
protected $customAttributes = [
'id' => 'User ID:{:id}',
];
If the id
is passed in as hello
At this point the trigger will prompt User ID: hello must be a number.
Formatting error messages in rule classes
In the appeal code, the error message may not be so clear. It may be better to put the $size
variable into the error message, and we provide the $messageParam
parameter to support formatting the error message, as follows:
class Length extends BaseRule
{
protected $message = 'The length of :attribute should be %d bytes';
protected $size;
public function __construct(int $size)
{
$this->size = $size;
$this->messageParam = [$size];
}
public function passes($attribute, $value): bool
{
return strlen($value) === $this->size;
}
}
$messageParam
is an array type, so the value passed in must be an array, such as using the rule length:10
, then the message after the trigger is: The length of :attribute should be 10 bytes
$message
Field Definition:
Formatting Format | Description |
---|---|
%% | Returns a percent sign % |
%b | Binary numbers |
%c | ASCII value corresponding to the character |
%d | Decimal numbers containing positive and negative signs (negative, 0, positive) |
%e | Use lowercase scientific notation (e.g. 1.2e+2) |
%E | Use uppercase scientific notation (e.g. 1.2E+2) |
%u | Decimal numbers without positive or negative signs (greater than or equal to 0) |
%f | Floating point (local setting) |
%F | Floating point (non-local setting) |
%g | Shorter %E and %f |
%G | Shorter %E and %f |
%o | Octal numbers |
%s | String |
%x | Hexadecimal numbers (lowercase letters) |
%X | Hexadecimal numbers (uppercase letters) |
Additional formatting values. Must be placed between % and a letter (e.g. %.2f).
\+
(Precede a number with + or - to define the positive or negative nature of the number. By default, only negative numbers are marked, and positive numbers are not marked.)'
(Specifies what to use as padding, the default is spaces. It must be used in conjunction with the width specifier. Example: %'x20s (uses "x" as padding))\-
(Left Adjusted Variable Value)[0-9]
(Specify the minimum width of the variable value).[0-9]
(Specify the number of decimal places or the maximum string length)
Note
If more than one of the above format values is used, the parameters of $messageParam
must be used in the above order, and must not be out of order.
For more support on error messages please see Custom Error Messages
Use of class attributes in rule classes
Sometimes we need to use a more intuitive way of defining an error message, and without formatting, we can use the properties of the rule class directly in the error message, in the format: %{field name}
Note
Only public
,protected
types are supported, and the value must be scalar
The rule in appeal code could be changed to read as follows.
class Length extends BaseRule
{
protected $message = 'The length of :attribute should be %{size} bytes';
protected $size;
public function __construct(int $size)
{
$this->size = $size;
}
public function passes($attribute, $value): bool
{
return strlen($value) === $this->size;
}
}
If you want to use a value under an array, you can use the same fetch method as the get method of the validator collection.
Error messages in the form of arrays
Error messages also support definitions of the following form
class TestArrayMessageValidate extends Validate
{
protected $rule = [
'user' => 'required|min:3|max:8',
'pass' => 'required|min:3|max:8',
];
protected $message = [
'user' => [
'required' => 'Username cannot be empty',
'min' => 'The length of the username must not be less than :min',
'max' => 'The length of the username must not exceed :max',
],
'pass' => [
'required' => 'Password cannot be empty',
'min' => 'The length of the password must not be less than :min',
'max' => 'The length of the password must not exceed :max',
]
];
}
The effect is equivalent to
class TestArrayMessageValidate extends Validate
{
protected $rule = [
'user' => 'required|min:3|max:8',
'pass' => 'required|min:3|max:8',
];
protected $message = [
'user.required' => 'Username cannot be empty',
'user.min' => 'The length of the username must not be less than :min',
'user.max' => 'The length of the username must not exceed :max',
'pass.required' => 'Password cannot be empty',
'pass.min' => 'The length of the password must not be less than :min',
'pass.max' => 'The length of the password must not exceed :max',
];
}