A
A
Alexey Verkhovtsev2019-01-29 17:12:10
symfony
Alexey Verkhovtsev, 2019-01-29 17:12:10

How to validate internal array in Symfony 4?

Hello! There is a request with something like this body

{
  "subject": "subj",
  "data": "text",
  "toEmail": [
    "sgs",
    "3434"
  ]
}

Validation goes like this
$constraint = new Assert\Collection(
            [
                'fields' => [
                    'subject' => [
                        new Assert\NotBlank(),
                        new Assert\Type('string'),
                        new Assert\Length(['min' => 2, 'max' => 255])
                    ],
                    'data' => [
                        new Assert\NotBlank(),
                        new Assert\Type('string'),
                        new Assert\Length(['min' => 2])
                    ],
                    'toEmail' => [
                        new Assert\NotBlank(),
                        new Assert\Type('array')
                    ]
                ]
            ]
        );
        $violations = $validator->validate($body, $constraint);

But how else can you check in a similar way that in the toEmail array there are exactly email addresses of a certain length, etc.?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BoShurik, 2019-01-29
@seftomsk

https://symfony.com/doc/current/reference/constrai...

'toEmail' => [
    new Assert\NotBlank(),
    new Assert\Type('array'),
    new Assert\All([
        'constraints' => [
            new Assert\NotBlank(),
            new Assert\Email(),
        ],
    ])
]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question