respect-validation/docs/rules/Lazy.md
Henrique Moody 0c07060a04
Rename "Consecutive" to "Circuit"
The "Consecutive" rule is now renamed to "Circuit" to better reflect its
behavior of stopping at the first failure. I also favour this name
because it's much shorter.
2024-12-20 16:53:56 +01:00

1.5 KiB
Raw Blame History

Lazy

  • Lazy(callable(mixed $input): Rule $ruleCreator)

Validates the input using a rule that is created from a callback.

This rule is particularly useful when creating rules that rely on the input. A good example is validating whether a confirmation field matches the password field when processing data from a form.

v::key('confirmation', v::equals($_POST['password'] ?? null))->isValid($_POST);

The issue with the code is that its hard to reuse because youre relying upon the input itself ($_POST). That means you can create a chain of rules and use it everywhere.

The lazy() rule makes this job much simpler and more elegantly:

v::lazy(static fn($input) => v::key('confirmation', v::equals($input['password'] ?? null)))->isValid($_POST);

The code above is similar to the first example, but the biggest difference is that the creation of the rule doesn't rely on the input itself ($_POST), but it will use any input thats given to the rule

Templates

Template placeholders

Placeholder Description
name The validated input or the custom validator name (if specified).

Categorization

  • Callables
  • Nesting

Changelog

Version Description
3.0.0 Created from KeyValue

See also: