Skip to content
This repository has been archived by the owner on Jul 6, 2024. It is now read-only.

Latest commit

 

History

History
45 lines (37 loc) · 884 Bytes

UniqueConstraint.md

File metadata and controls

45 lines (37 loc) · 884 Bytes

UniqueConstraint

<?php

use Chubbyphp\Validation\Constraint\UniqueConstraint;
use Chubbyphp\Validation\ValidatorContextInterface;
use Doctrine\Common\Persistence\ObjectManager;

/** @var ObjectManager $objectManager */
$objectManager = ...;

$constraint = new UniqueConstraint($objectManager, ['name']);

/** @var ValidatorContextInterface $context */
$context = ...;

// Use NotNullConstraint to prevent null
$errors = $constraint->validate(
    'path.to.model',
    null,
    $context
);
// [];

$errors = $constraint->validate(
    'path.to.model',
    $model,
    $context
);
// [];

// There is already a model with the same values
$errors = $constraint->validate(
    'path.to.model',
    $model,
    $context
);
// [
//     new Error(
//         'path.to.model.name',
//         'constraint.unique.notunique',
//         ['uniqueProperties' => 'name']
//     )
// ];