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;
}
1
2
3

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);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

Output

array(1) {
  'id' =>
  array(4) {
    [0] =>
    int(1)
    [1] =>
    int(2)
  }
}
1
2
3
4
5
6
7
8
9

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);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

Output

array(1) {
  'id' =>
  array(2) {
    [0] =>
    int(1)
    [1] =>
    int(2)
  }
}
1
2
3
4
5
6
7
8
9

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);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

Output

array(1) {
  'id' =>
  array(4) {
    [0] =>
    int(1)
    [1] =>
    int(2)
    [4] =>
    int(3)
    [6] =>
    int(4)
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13

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);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

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.

Last Updated: 8/19/2021, 4:54:27 PM