Search code examples
regexsymfonyvalidationassert

Symfony assert: Type is array of str, which each str being check using regex


Just wanted to know if this kind of assert exist in symfony (If not I will create it for my project).

Like I have an assert

#[AssertArrayRegex('/[0-9]{2}/')]
public array $variables = [];

This array would be validated

$array = [
'12',
'13'
]

But not this one

$array = [
'12',
'1'
]

Solution

  • you can put this code on your Entity Class :

    #[Assert\All([
            new Assert\NotBlank,
            new Assert\Length(min: 2),
    ])]
    $your_variable;
    

    Or maybe this one :

    #[Assert\All([
            new Assert\Regex('/[0-9]{2}/')
    ])]
    $your_variable;
    

    Regards