Simple Validate
Support for simply defining a validator and performing validation:
try {
$data = Validate::make([
'user' => 'required|email',
'pass' => 'required|lengthBetween:6,16',
], [
'user.required' => 'Please enter your username',
'user.email' => 'User name format error',
'pass.required' => 'Please enter your password',
'pass.lengthBetween' => 'Password length is 6~16 bits',
])->check($data);
} catch (ValidateException $e) {
echo $e->getMessage();
}
If the validation passes, all values that passed the validation are returned, if not, a Itwmw\Validate\Exception\ValidateException
exception is thrown
Validator Definition
To define the validator class for a specific validation scenario or data form, we need to inherit the Itwmw\Validate\Validate
class, Then instantiate and call the check
method of the validation class directly to complete the validation, an example of which is as follows.
Define a LoginValidate
validator class for login validation.
class LoginValidate extends Validate
{
protected $rule = [
'user' => 'required|email',
'pass' => 'required|digits_between:6,16',
];
protected $message = [
'user.required' => 'Please enter your username',
'user.email' => 'Incorrect username format',
'pass.required' => 'Please enter your password',
'pass.digits_between' => 'Password length of 6 to 16 digits',
];
}
Error messages defined by class attributes take precedence over the default responses in custom rules and over the errors returned by custom rule methods.
Data Validate
$data = [
'user' => '123@qq.com',
'pass' => ''
];
$validate = new LoginValidate();
$validate->check($data);
This throws a Itwmw\Validate\Exception\ValidateException
exception with the message Please enter your password
$data = [
'user' => '123@qq.com',
'pass' => '123456'
];
$validate = new LoginValidate();
$data = $validate->check($data);
Validates successfully and returns the validated value, which is an array type
Validate Arrays
It is not difficult to validate the form input as an array of fields. You can use the "dot" method to validate properties in an array. For example, if the incoming HTTP request contains the search[keyword]
field, it can be validated as follows.
protected $rule = [
'search.order' => 'numeric|between:1,2',
'search.keyword' => 'chsAlphaNum',
'search.recycle' => 'boolean',
];
You can also validate each element in an array. For example, to validate that each id in a given array input field is unique, you can do this.
protected $rule = [
'search.*.id' => 'numeric|unique:account'
];
The definition of an error message for an array rule is the same
protected $message = [
'search.order.numeric' => 'order parameter error',
'search.order.between' => 'order parameter error',
'search.keyword.chsAlphaNum' => 'Keywords can only contain Chinese, letters, numbers',
'search.recycle.boolean' => 'Parameter error:recycle',
];
On the other hand, if your field name contains a literal period, you can explicitly prevent this from being interpreted as "dot" syntax by escaping the period with a backslash:
protected $rule = [
'a\.1' => 'required'
];
Validator Class Attributes
$rule
The validation rules of the user-defined validator can also be set via the setRules
method, This method will superimpose the data.If the parameter is null
then it is clear all rules.
// Definition in class
protected $rule = [
'user' => 'required'
];
// Definition of usage methods
$v->setRules([
'user' => 'required'
]);
$message
User-defined validator error messages can also be set via the setMessages
method, This method will superimpose the data,If the parameter is null
then it is clear all error messages.
// Definition in class
protected $message = [
'user.required' => 'user is required'
];
// Definition of usage methods
$v->setMessages([
'user.required' => 'pass is required'
]);
For more usage examples, please refer to the Message section.
$scene
Define the data of the validation scenario, which is used to specify the validation fields corresponding to the validation scenario, etc. See the section on validate scene for detailed usage.
The same can be done with the setScene
method. This method will superimpose the data, If the parameter is null
, then all validate scene are cleared.
// Definition in class
protected $scene = [
'login' => ['user', 'pass']
];
// Definition of usage methods
$v->setScene([
'login' => ['user', 'pass']
]);
$event
Define global events under this validator, see the section Events for detailed usage.
protected $handler = [
CheckSiteStatus::class
];
$preprocess
Defines a preprocessing handler for a field.
protected $preprocessor = [
'name' => ['Default Name', ProcessorExecCond::WHEN_EMPTY]
];
For more details about preprocessing handlers, please refer to the Processor section.
$postprocessor
Defines a postprocessing handler for a field.
protected $postprocessor = [
'name' => ['trim', ProcessorExecCond::WHEN_NOT_EMPTY, ProcessorParams::Value]
];
For more details about postprocessing handlers, please refer to the Processor section.
$customAttributes
Define the name of the validation field, also can be set by setCustomAttributes
method, this method will superimpose the data, if the parameter is null
then it is clear all field names. the :attribute in the error message will be replaced with the value corresponding to the following
protected $customAttributes = [
'user' => 'Account',
'pass' => 'Password'
];
$ruleMessage
The error message for failed validation of a custom method in the current class.
protected $ruleMessage = [
'The value of :attribute can only have Chinese'
];
Click to view example
$filled
All validated fields cannot be empty when they exist, if the value of this property is true
, all rules will automatically add filled
rules, the default is true
The filled
rule is not automatically added when:
- Validation rules exist in
filled
,nullable
,accepted
,declined
,declined_if
,present
,required
,required_if
,required_unless
,required_with
,required_with_all
,required_without
,required_without_all
Rule - Validation rules exist extendImplicit Defined rules
- The validation rules implement the
Itwmw\Validation\Support\Interfaces\ImplicitRule
Marker Interface
protected bool $filled = true;
$regex
Predefined regular expression validation rules, see Regular Expression Rule for details
protected $regex = [
'number' => '/^\d+$/'
];
$group
Define validation rule groups
protected $group = [
'username' => 'alpha_num|min:5|max:10'
];
protected $rule = [
'username' => 'required|username'
];
Can also use array format:
protected $group = [
'username' => [
'alpha_num',
'min:5',
'max:10'
]
];
protected $rule = [
'username' => 'required|username'
];
$exceptions
Throw a custom exception class when validation fails for a specified field or specified field's specified rules.
protected $rule = [
'name' => 'required'
];
protected $exceptions = [
TestException::class => 'name.required'
];
For multiple fields or multiple rules:
protected $rule = [
'name' => 'required|integer',
'age' => 'required|integer'
];
protected $exceptions = [
TestException::class => [
'name',
'age.required'
]
];
Throw a TestException
exception when the validation fails for all rules of the name
field and the required
rule of the age
field.
protected $rule = [
'name' => 'required|integer',
'age' => 'required|integer'
];
protected $exceptions = TestException::class;
Upon validation failure by this validator, a TestException
exception will be thrown.