Micro Dream Form ValidateMicro Dream Form Validate
Home
Plugin
Video Tutorial
Source Code
  • 6.x
  • 5.x
  • 简体中文
  • English
Home
Plugin
Video Tutorial
Source Code
  • 6.x
  • 5.x
  • 简体中文
  • English
  • Introduction
  • Guide

    • Start
    • Validator
    • Validate Scene
    • Validation Events
    • Built in rules
    • Custom Rules
    • Custom Error Messages
    • Data Processor
    • Validator Collection
  • Update log

If you need to dynamically fetch data during rule definitions, you can utilize this parser.

use Itwmw\Validate\Support\Traits\RuleParamsParser;
use Itwmw\Validate\Validate;

class Validator extends Validate
{
    use RuleParamsParser;
}

The Rule Parameters Parser facilitates several functionalities:

  • Parsing method calls within parameters
  • Parsing property retrievals within parameters
  • Parsing environment variables within parameters
  • Parsing field references within parameters

Note The following examples are for illustration purposes and do not account for practical usage scenarios.

Method Invocation

Use {#function} to invoke methods, including global functions, methods within the current class, or static methods in a specified class.

class Validator extends Validate
{
    use RuleParamsParser;

    protected $rule = [
        'password' => 'required|max:{#getPasswordMaxSize}'
    ];

    public function getPasswordMaxSize(): int
    {
        return 18;
    }
}

Supports calling static methods from a specified class using the full namespace, for example: max:{#App\\Helper\\Constant::getPasswordMaxSize}

Passing Parameters

If the method requires parameters, you can add them after the method name, like this: {#getPasswordMaxSize(18)}. These parameters also support using property retrievals, environment variables, and field references.

Property Retrieval

Retrieve properties of the current class using {->property}. For multi-level class references, use ->; for array references, use .. Examples: {->user->name} and {->user.info->nickname}.

class Validator extends Validate
{
    use RuleParamsParser;

    protected int $passwordMaxSize = 18;

    protected $rule = [
        'password' => 'required|max:{->passwordMaxSize}'
    ];
}

Environment Variables

Fetch values from environment variables using {@env}.

putenv('PASSWORD_MAX_SIZE=18');

class Validator extends Validate
{
    use RuleParamsParser;

    protected int $passwordMaxSize = 18;

    protected $rule = [
        'password' => 'required|max:{@PASSWORD_MAX_SIZE}'
    ];
}

Field References

To get the value of a field, use {:field}.

class Validator extends Validate
{
    use RuleParamsParser;

    protected int $passwordMaxSize = 18;

    protected $rule = [
        'password_length' => 'required|integer|min:6|max:20',
        'password' => 'required|max:{:password_length}'
    ];
}

Supports referencing array elements with syntax: {:field.field.index...}.