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

Latest commit

 

History

History
64 lines (55 loc) · 1.11 KB

ChoiceConstraint.md

File metadata and controls

64 lines (55 loc) · 1.11 KB

ChoiceConstraint

<?php

use Chubbyphp\Validation\Constraint\ChoiceConstraint;
use Chubbyphp\Validation\ValidatorContextInterface;

$constraint = new ChoiceConstraint(['active', 'inactive']);

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

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

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

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

$errors = $constraint->validate(
    'path.to.property',
    [],
    $context
);
// [
//     new Error(
//         'path.to.property',
//         'constraint.choice.invalidtype',
//         ['type' => 'array']
//     )
// ];

$errors = $constraint->validate(
    'path.to.property',
    'choice',
    $context
);
// [
//     new Error(
//         'path.to.property',
//         'constraint.choice.invalidformat',
//         [
//             'value' => 'choice',
//             'choices' => ['active', 'inactive']
//           ]
//     )
// ];