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

Latest commit

 

History

History
77 lines (66 loc) · 1.31 KB

File metadata and controls

77 lines (66 loc) · 1.31 KB

NumericRangeConstraint

<?php

use Chubbyphp\Validation\Constraint\NumericRangeConstraint;
use Chubbyphp\Validation\ValidatorContextInterface;

$constraint = new NumericRangeConstraint(1, 2);

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

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

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

// Use NumericConstraint to prevent not numeric
$errors = $constraint->validate(
    'path.to.property',
    'test',
    $context
);
// [];

$errors = $constraint->validate(
    'path.to.property',
    0,
    $context
);
// [
//     new Error(
//         'path.to.property',
//         'constraint.numericrange.outofrange',
//         ['value' => 0, 'min' => 1, 'max' => 2]
//     )
// ];

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

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

$errors = $constraint->validate(
    'path.to.property',
    3,
    $context
);
// [
//     new Error(
//         'path.to.property',
//         'constraint.numericrange.outofrange',
//         ['value' => 3, 'min' => 1, 'max' => 2]
//     )
// ];