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
//)







 
 
 
 









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] => 0
//)

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

Multiple definitions

In addition to writing a value directly as a default value, default values are also supported to be defined in the following ways

Closures

The default value supports closures, which will receive four values

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

The style of the callback method is as follows:

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

Example:

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

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 four 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';
}

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.

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';
}

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'];
    }
}

Using:

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

    protected $default = [
        'id' => DefaultUserId::class
    ];
}

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.

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],
    ];
}

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