FieldReferencingRuleInterface#

Meritum\Validation\FieldReferencingRuleInterface

Extends RuleInterface . Rules that implement this interface receive a field name as their schema parameter and have the target field’s value resolved from the input before validate() is called.

sameAs and differentFrom implement this interface.

Methods#

/**
 * @param array<string, mixed> $input
 * @return mixed[]
 */
public function resolveParams(string $attribute, array $input): array;

Called by the engine before validate(). $attribute is the target field name taken from the schema (e.g. 'password' for 'sameAs' => 'password'). Return the array of params to be spread into validate() and message().

Typically this returns the resolved value alongside the field name for use in error messages:

public function resolveParams(string $attribute, array $input): array
{
    return [$input[$attribute] ?? new Missing(), $attribute];
}

The engine validates that the schema param exists and is a string before calling resolveParams(). If it is missing or non-string, an InvalidArgumentException is thrown.

Example#

use Meritum\Validation\FieldReferencingRuleInterface;
use Meritum\Validation\Missing;

final class GreaterThanFieldRule implements FieldReferencingRuleInterface
{
    public function name(): string
    {
        return 'greaterThanField';
    }

    public function resolveParams(string $attribute, array $input): array
    {
        return [$input[$attribute] ?? new Missing(), $attribute];
    }

    public function validate(mixed $value, mixed ...$params): bool
    {
        return is_numeric($value) && is_numeric($params[0]) && $value > $params[0];
    }

    public function message(string $attribute, mixed ...$params): string
    {
        return "The {$attribute} must be greater than {$params[1]}";
    }
}

Usage:

'end_date' => ['required', 'date', 'greaterThanField' => 'start_date'],