Assign default values to null values, execute before validation, and determine null when the value is an empty array, a null character, and null.

Note

Setting default values for array elements is not supported, i.e.: setting default values for id.* is not supported

Using

Class property definition

Use the $default parameter of the class to set default values for the fields

class Setting extends Validate
{
    protected $rule = [
        'site_status'     => 'required|in:0,1',
        'register_status' => 'required|in:0,1'
    ];

    protected $default = [
        'site_status'     => 1,
        'register_status' => 1
    ];
}

$data = Setting::make()->check([]);
print_r($data);
//Array
//(
//    [site_status]     => 1
//    [register_status] => 1
//)







 
 
 
 









1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

Use in validator scene

Add a base validation scene to the class

class Setting extends Validate
{
    protected $rule = [
        'site_status'     => 'required|in:0,1',
        'register_status' => 'required|in:0,1'
    ];

    protected $default = [
        'site_status'     => 1,
        'register_status' => 1
    ];

    protected function sceneBase(ValidateScene $scene)
    {
        $scene->only(['site_status'])
            ->default('site_status', 0);
    }
}
$data = Setting::make()->scene('base')->check([]);
print_r($data);
//Array
//(
//    [site_status] => 1
//)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

Description

A field can only have one default value. Once the default value in the validation scenario takes effect, the globally defined default value becomes invalid.

The default value only takes effect for the field that currently needs to be validated

Variety of defaults

Closures

The default value supports closures, which will receive three values

  • $value The data of the current field
  • $attribute Current field name
  • $originalData All the original data of the current validation

The style of the callback method is as follows:

function($value, string $attribute, array $originalData) {
    return $value;
}
1
2
3

Example:

$scene->defalt('name', function($value) {
    return 'Emma';
});
1
2
3

Delete field

If you want to delete a field, the validator will pass in a DataAttribute object in the fourth field of the method, and you can set the deleteField property of this object to true.

Class Methods

When using class methods to pass default values for a given field, the same three values are accepted

  • $value The data of the current field
  • $attribute Current field name
  • $originalData All the original data of the current validation
  • $dataAttribute Data attributes
$scene->defalt('name', [$this,'setDefaultName']);

public function setDefaultName($value, string $attribute, array $originalData)
{
    return 'Emma';
}
::: tip Delete field
If you want to delete a field, the validator will pass in a `DataAttribute` object in the fourth field of the method, 
and you can set the `deleteField` property of this object to `true`.
:::
1
2
3
4
5
6
7
8
9
10

Simple Method:

Define a default value method (the naming convention for methods is default + Initial capitalization of the name)

$scene->defalt('name','setDefaultName');

public function defaultSetDefaultName($value, string $attribute, array $originalData)
{
    return 'Emma';
}
1
2
3
4
5
6

Ignore null condition

When the value is an empty array, a null character and null, the corresponding default value will be taken out and assigned to the original data. If you want any value to get the default value once, you can add the condition any => true. If you add any, you need to put your default value into value as follows.

class Setting extends Validate
{
    protected $rule = [
        'site_status' => 'required|in:0,1',
    ];

    protected $default = [
        'site_status' => ['value' => 1, 'any' => true],
    ];
}
1
2
3
4
5
6
7
8
9
10

At this point, whatever the original parameter of site_status is, it will be reset to 1, and you can also use this feature to do things like advance formatting for the parameter

Cancel default value

If you want to cancel the default value set in the class property in the validation scenario, you can set the value to null, as follows:

class User extends Validate
{
    protected $rule = [
        'name' => ''
    ];

    protected $default = [
        'name' => 'Emma'
    ];

    protected function sceneTest(ValidateScene $scene)
    {
        $scene->only(['name'])
            ->default('name', null);
    }
}

$data = User::make()->scene('test')->check([]); // Return the empty array
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

Classes with default values

To create a class that provides default values, you need to implement the W7\Validate\Support\Concerns\DefaultInterface interface.

class DefaultUserId implements DefaultInterface
{
    public function handle($value, string $attribute, array $originalData)
    {
        return $_SESSION['user_id'];
    }
}
1
2
3
4
5
6
7

Using:

class UserValidate extends Validate
{
    protected $rule = [
        'id'   => 'required',
    ];

    protected $default = [
        'id' => DefaultUserId::class
    ];
}
1
2
3
4
5
6
7
8
9
10

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.

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