Skip to content

Fluent API overview

Fynix rules are configured through an immutable fluent API. Start with a rule from the Rule facade, then chain constraints that describe the value or object field you want to validate.

use Fynix\Rule;
$email = Rule::email('email')
->required()
->max(180);
$error = $email->validate('not-an-email');

Each configuration method returns a new rule. The original rule is unchanged, which makes it safe to reuse a base definition:

$baseName = Rule::string('name')->min(2)->max(80);
$requiredName = $baseName->required();
$optionalName = $baseName->optional();

Use a standalone rule when you already have a scalar value:

$error = Rule::string('firstName')
->min(2)
->validate('A');

Use Rule::for($object) when the value belongs to an object instance:

$error = Rule::for($user)
->string('firstName')
->min(2)
->validate();

Use Rule::on(User::class) or a RuleSet when defining reusable class-scoped rules:

$rules = [
Rule::on(User::class)->string('firstName')->min(2),
Rule::on(User::class)->email('email')->max(180),
];

Use ValidationHandler::validate() when running several rules against a complete object. The handler calls the rules internally; do not add ->validate() inside its rules array.

GroupMethodsUse for
Presentationlabel()Human-readable error labels
Presencerequired(), optional()Empty and missing values
Boundsmin(), max(), length()Length, numeric, or collection limits
Value setsin(), notIn()Allow-lists and deny-lists
Cross-fieldsameAs(), differentFrom()Comparing fields on one object
Conditional presencerequiredIf(), requiredUnless()Requiring fields based on another field or closure
Conditional prohibitionprohibitedIf(), prohibitedUnless()Rejecting fields based on a field or closure
Conditional executionwhen()Running a complete validator only when a closure matches
Pipeline controlsgenericValidation(), withoutGenericValidation()Shared validation behavior
Executionvalidate(), validateAll()Running one rule

Every method is documented in detail in this section. See Rules, RuleSet, and registry for construction and registration, or begin with label().

Conditional methods accept either a field/value pair or a closure receiving the object being validated. sameAs() and differentFrom() accept closures that return the comparison value. Use when() to conditionally skip the entire validator pipeline.