Skip to content

Rule Precedence

When using fluent rules in the ValidatableBuilder the order of the rules is automatically determined.

The logic follows a two-step process:

  • Resolve the rule type
  • Resolve priority based on the resolved type

Resolve type

Based on the type_to_rule_map as defined in the configuration file the RuleTypeResolver tries to resolve a type. When unable to resolve a type it defaults to the default_rule_type as defined in the same configuration file.

Resolve priority

After the RuleTypeResolver determined the type, the RulePriorityResolver tries to determine the priority. It does that using the type_to_priority_map as defined in the configuration file. When unable to resolve a priority it defaults to the default_rule_priority as defined in the same configuration file.

By default, execution priority follows this order: modifier -> circuit -> presence -> type -> constraint

Custom type resolving

Custom rule type resolving can be accomplished in two ways:

  • customize type_to_rule_map and/or default_rule_type configuration
  • create a custom RuleTypeResolver that implements LaraPkgs\Validation\Contracts\RuleTypeResolver and bind it in the container
php
<?php

declare(strict_types=1);

namespace App\Validation\Resolvers;

use LaraPkgs\Validation\Contracts\RuleTypeResolver;

final class CustomRuleTypeResolver implements RuleTypeResolver
{
    public function resolve(string $ruleName): string
    {
        // TODO: Implement resolve() method.
    }
}
php
<?php

declare(strict_types=1);

namespace App\Providers;

use App\Validation\Resolvers\CustomRuleTypeResolver;
use Illuminate\Support\ServiceProvider;
use LaraPkgs\Validation\Contracts\RuleTypeResolver as RuleTypeResolverContract;

final class AppServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        $this->app->singleton(RuleTypeResolverContract::class, function ($app) {
            return new CustomRuleTypeResolver;
        });
    }
}

Custom priority resolving

Custom rule priority resolving can be accomplished in two ways:

  • customize type_to_priority_map and/or default_rule_priority configuration
  • create a custom RulePriorityResolver that implements LaraPkgs\Validation\Contracts\RulePriorityResolver and bind it in the container
php
<?php

declare(strict_types=1);

namespace App\Validation\Resolvers;

use LaraPkgs\Validation\Contracts\RulePriorityResolver;

final class CustomRulePriorityResolver implements RulePriorityResolver
{
    public function resolve(string $type): int
    {
        // TODO: Implement resolve() method.
    }
}
php
<?php

declare(strict_types=1);

namespace App\Providers;

use App\Validation\Resolvers\CustomRulePriorityResolver;
use Illuminate\Support\ServiceProvider;
use LaraPkgs\Validation\Contracts\RulePriorityResolver as RulePriorityResolverContract;

final class AppServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        $this->app->singleton(RulePriorityResolverContract::class, function ($app) {
            return new CustomRulePriorityResolver;
        });
    }
}