Questions tagged [symfony-validator]

Symfony component which provides "validation constraints", which are simple objects containing the rules for the validation.

132 questions
28
votes
1 answer

Why do I receive "This value should be of type string" when using a DateTime constraint on Symfony 5?

I have the following entity (only attached the relevant parts): use ApiPlatform\Core\Annotation\ApiResource; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; /** * @ApiResource(mercure=true) *…
Toma
  • 2,764
  • 4
  • 25
  • 44
15
votes
3 answers

Conditional validation of fields based on other field value in Symfony2

So here is the scenario: I have a radio button group. Based on their value, I should or shouldn't validate other three fields (are they blank, do they contain numbers, etc). Can I pass all these values to a constraint somehow, and compare them…
spiridon
  • 323
  • 1
  • 2
  • 10
8
votes
2 answers

Use UniqueEntity outside of entity and without forms

I need to validate an email passed by user: private function validate($value): bool { $violations = $this->validator->validate($value, [ new Assert\NotBlank(), new Assert\Email(), new UniqueEntity([ …
Petr Flaks
  • 543
  • 2
  • 7
  • 25
8
votes
3 answers

How to validate if an element of an array is an array itself?

Given this input: [ 'key' => 'value', ] How to validate to ensure that: key attribute exists Its value is an array (with any number of elements) I expected this constraint to work $constraint = new Collection([ 'key' => new…
zerkms
  • 249,484
  • 69
  • 436
  • 539
7
votes
1 answer

Unable to use Callback assert with a form without data_class

I'm creating a custom FormType called IntervalType. My IntervalType will have two fields, start and end and will be of type integer. This custom FormType will always be used without data_class. I want to add a constraint to guarantee that start is…
murilolobato
  • 349
  • 4
  • 14
6
votes
2 answers

Validate against several constraints with OR logic in Symfony

I have a field in a request which can either be email or phone. Now I need to validate it against two constraints - the standard email constraint and a custom phone. I know that I can pass an array of constrains link this: $constraint = new…
super.t
  • 2,526
  • 7
  • 32
  • 51
6
votes
5 answers

ConstraintViolationListInterface to Exception in Symfony

I need to convert an object of type ConstraintViolationListInterface to a single exception for further logging, where the message is a concatenation of the messages from each constraint violation on the list, when the validation fails. Obviously I…
super.t
  • 2,526
  • 7
  • 32
  • 51
6
votes
2 answers

How to compare dates in validation?

I'm trying to compare dates in my validation. The documentation says it's possible but It's not documented. I'm using annotations and I want one date to be later that the other. How do I do this?
SnelleJelle
  • 933
  • 5
  • 18
  • 35
6
votes
3 answers

How to disable one transformer validation error "dynamically" in Symfony2

I have a form with many fields and validation groups, these fields contain some view data transformers too. I need suppress the validation form partially (Groups based on the Submitted Data): use AppBundle\Entity\Client; use…
yceruto
  • 9,230
  • 5
  • 38
  • 65
5
votes
3 answers

Symfony UniqueEntity shows an error on when updating existing entity

I have a fairly simple entity with UniqueEntity validation: namespace App\Entity; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Security\Core\User\UserInterface; use Doctrine\ORM\Mapping\ManyToOne; use…
jdog
  • 2,465
  • 6
  • 40
  • 74
5
votes
3 answers

Symfony3 form validation not called for field use by a model transformer

Edit: here's a github with the full code to reproduce the problem I have the following entity class Place { use Traits\HasId; /** * Used for form. * * @Assert\Image( * mimeTypes = {"image/png", "image/jpeg"}, *…
allan.simon
  • 3,886
  • 6
  • 35
  • 60
5
votes
1 answer

How to use group sequence on form type in symfony forms?

The idea is to first validate if all the required fields are not blank. If all the required data is provided then validate if the values entered are correct. The usual case for groups sequence. How ever when I apply new GroupSequence(["Basic",…
4
votes
1 answer

Symfony "fos_rest.request_body" converter: do not deserialize nested DTO classes

I have a OrderDto class with a nested PointDto class (array of points): class OrderDto { /** * @var PointDto[] * @Assert\All({ * @Assert\Type("App\Dto\PointDto") * }) * @Assert\Valid() */ private array…
inkrot
  • 448
  • 5
  • 12
4
votes
1 answer

Symfony validation on multiple file upload

I have a form containing a FileType field. I've set the multiple option to true so the user can upload multiple files at the same time. $builder->add('myFile', FileType::class, [ 'label' => 'upload file', 'multiple'…
MehdiB
  • 870
  • 12
  • 34
4
votes
1 answer

Symfony Regex validator doesn't work in annotation

I'm building an application on Symfony 3.2 On one part of the application I'm providing an interface my users to change their passwords. For this task, I have a simple form which bound to my ChangePasword entity as follows. Form class: namespace…
Lashae
  • 1,372
  • 1
  • 20
  • 36
1
2 3
8 9