Oihana PHP

UniqueModelRule extends ExistModelRule

Rule: Validates that a given value is **unique** within a model managed by the DI container.

This rule extends ExistModelRule and inverts its logic: it passes only if the provided value does not already exist in the target model.

The model must implement ExistModel, which defines the exist(array $criteria): bool method used for lookups.


Usage

use oihana\validations\rules\models\UniqueModelRule;
use Somnambulist\Components\Validation\Validator;
use Psr\Container\ContainerInterface;

// Assume $container provides access to models implementing ExistModel.

$rule = new UniqueModelRule
(
    $container,
    [
        UniqueModelRule::MODEL => 'user.model',
        UniqueModelRule::KEY   => 'email',
    ]
);

$validator = new Validator
(
    ['email' => 'john@example.com'],
    ['email' => [$rule]]
);

$validator->passes(); // true if no existing user with this email
$validator->fails();  // true if a user with this email already exists

Behavior

  • Inherits all initialization logic from ExistModelRule.
  • Calls ExistModelRule::check() internally and returns its logical negation.
  • Requires that:
    • the model exists in the DI container,
    • the model implements ExistModel,
    • the model correctly responds to exist([ModelParam::KEY => ..., ModelParam::VALUE => ...]).

Custom Error Messages

$rule = new UniqueModelRule($container, 'user.model', 'email');
$rule->message(':attribute already exists in :model.');

Tags
see
ExistModelRule

The base class that checks for existence in a model.

ExistModel

Interface that must be implemented by model classes.

author

Marc Alcaraz

since
1.0.0

Table of Contents

Constants

DEFAULT_KEY  : string = \org\schema\constants\Schema::ID
The default 'key' value.
KEY  : string = 'key'
The 'key' parameter key.
MODEL  : string = 'model'
The 'model' parameter key.

Properties

$container  : ContainerInterface
The DI container reference.
$fillableParams  : array<string|int, mixed>
The internal list of fillable parameters.
$message  : string
The internal message pattern.

Methods

__construct()  : mixed
Creates a new ExistModelRule instance.
check()  : bool
Checks whether the given value satisfies the condition.
key()  : $this
Defines the optional key to find the ressource in the model.
model()  : $this
Defines the model identifier to find it in the DI container.

Constants

DEFAULT_KEY

The default 'key' value.

public string DEFAULT_KEY = \org\schema\constants\Schema::ID

Properties

$container

The DI container reference.

protected ContainerInterface $container

$fillableParams

The internal list of fillable parameters.

protected array<string|int, mixed> $fillableParams = [self::MODEL, self::KEY]

$message

The internal message pattern.

protected string $message = ":attribute must be unique in the model ':model', the value ':value' already exist."

Methods

__construct()

Creates a new ExistModelRule instance.

public __construct(ContainerInterface $container[, array<string|int, mixed>|string $init = [] ][, string|null $key = null ]) : mixed
Parameters
$container : ContainerInterface

The DI container reference.

$init : array<string|int, mixed>|string = []

The options to passed-in the rule.

$key : string|null = null
Tags
throws
ContainerExceptionInterface

If the container encounters an error while retrieving the entry.

NotFoundExceptionInterface

If no entry was found for the given identifier in the container.

check()

Checks whether the given value satisfies the condition.

public check(mixed $value) : bool
Parameters
$value : mixed

The value to check.

Tags
throws
ContainerExceptionInterface

If the container encounters an error while retrieving the entry.

NotFoundExceptionInterface

If no entry was found for the given identifier in the container.

ParameterException

If a required rule parameter is missing or invalid.

Return values
bool

True if the value satisfies the condition.

key()

Defines the optional key to find the ressource in the model.

public key([string|null $value = null ]) : $this
Parameters
$value : string|null = null

The key value.

Return values
$this

Returns $this to allow method chaining.

model()

Defines the model identifier to find it in the DI container.

public model([string|null $value = null ]) : $this
Parameters
$value : string|null = null

The identifier of the model definition in the DI container.

Return values
$this

Returns $this to allow method chaining.

On this page

Search results