Apply a filter to the input value after validation and assign it back to the original property variable after validation.
Description
This can be a global function name, filter class,an anonymous function, etc. The function signature must be:
function ($value) {
return $newValue;
}
There are many PHP functions that have the signature expected for the filter
callback. For example to apply type casting (using e.g. intval
, boolval
, ...) to ensure a specific type for an attribute, you can simply specify the function names of the filter without the need to wrap them in a closure.
Using
Class property definition
class UserValidate extends Validate
{
protected $rule = [
'id' => 'required|array',
'id.*' => 'numeric'
];
protected $filter = [
'id.*' => 'intval'
];
}
$data = UserValidate::make()->check([
'id' => ['1', 2]
]);
var_dump($data);
Output
array(1) {
'id' =>
array(4) {
[0] =>
int(1)
[1] =>
int(2)
}
}
Use in validator scene
class UserValidate extends Validate
{
protected $rule = [
'id' => 'required|array',
'id.*' => 'numeric'
];
protected function sceneToInt(ValidateScene $scene)
{
$scene->only(['id', 'id.*'])
->filter('id.*', 'intval');
}
}
$data = UserValidate::make()->scene('toInt')->check([
'id' => ['1', 2]
]);
var_dump($data);
Output
array(1) {
'id' =>
array(2) {
[0] =>
int(1)
[1] =>
int(2)
}
}
Multiple definitions
Filter handling is defined in the following ways, in addition to using PHP's built-in methods directly.
Closures
Filters also support closures, which will receive two values
$value
The data of the current field $dataAttribute
Data attribute
The style of the callback method is as follows:
function($value, DataAttribute $dataAttribute) {
return $newValue;
}
Example:
$scene->filter('name', function ($value, DataAttribute $dataAttribute) {
return removeXss($value);
});
Filter class
To create a filter class, you need to implement the W7\Validate\Support\Concerns\FilterInterface
interface:
class UniqueFilter implements FilterInterface
{
public function handle($value)
{
return array_unique($value);
}
}
class UserValidate extends Validate
{
protected $rule = [
'id' => 'required|array',
'id.*' => 'numeric'
];
protected $filter = [
'id' => UniqueFilter::class
];
}
$data = UserValidate::make()->scene('toInt')->check([
'id' => [1,2,1,2,3,3,4]
]);
var_dump($data);
Output
array(1) {
'id' =>
array(4) {
[0] =>
int(1)
[1] =>
int(2)
[4] =>
int(3)
[6] =>
int(4)
}
}
Delete field
If you want to delete a field, in the constructor method, the validator will pass in the DataAttribute
object, and you can set the deleteField
property of this object to true
.
Class Methods
Filters also support direct writing to class methods, (the naming convention for methods is filter
+ Name (first letter needs to be capitalized))
class UserValidate extends Validate
{
protected $rule = [
'id' => 'required|array',
'id.*' => 'numeric'
];
protected $filter = [
'id' => 'uniqueFilter'
];
public function filterUniqueFilter($value)
{
return array_unique($value);
}
}
In the validator scene you can use [$this,'filterUniqueFilter']
to pass callable
directly
Delete field
If you want to delete a field, the validator will pass in a DataAttribute
object in the second field of the method, and you can set the deleteField
property of the object to true
.