Symfony Authentication

I work in a bundle where the user creates an “agreement” using the form, and I'm trying to verify that the user still has “credit”. Therefore, I created a custom validator that requests past messages and throws an error if the loan is not enough.

My problem is that if the user sends a date with the wrong format in the "date" field (i.e. 32-13-20122 24:05), Symfony still tries to do my own check, and I get all kinds of errors ( because $comision->getDate() is null , not a valid DateTime object).

I get this error:

clone method called by non-object

I can also check if the value of $comision->getDate() valid date-time in my custom validator, but it seems to me that this is not necessary since I added these rules to the date property.

This is my essence (simplified)

 /** * @MyValidation\TotalHours() */ class Comision { /** * @ORM\Column(type="datetime") * @Assert\DateTime() * @Assert\NotNull() */ protected $date; /** * @ORM\Column(type="decimal", nullable=false, scale=1) * @Assert\NotBlank() */ protected $hours; ... 

My form class ...

 class NewComisionType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('date', DateTimeType::class, array( 'widget' => 'single_text', 'label' => 'Starting date and time', 'format' => 'dd/MM/yyyy HH:mm' )) ->add('hours', ChoiceType::class, array( 'label'=> 'How many hours', 'choices' => array( '1:00' => 1, '1:30' => 1.5, '2:00' => 2, '2:30' => 2.5, '3:00' => 3 ) )) ... 

And my crit validator, which checks past agreements to find if the user still has a “credit”

 public function validate($comision, Constraint $constraint) { $from = clone $comision->getDate(); $from->modify('first day of this month'); $to = clone $comision->getDate(); $to->modify('last day of this month'); $credit = $this->em->getRepository("ComisionsBundle:Comision")->comisionsByDate($comision,$from, $to); ... 
+7
php validation symfony
source share
3 answers

One way would be to group your constraints as described in the docs .

Thus, you can identify two groups of contraindications, while the second group will be checked only if all the restrictions in the first group are valid.

As for your use case, you can put your custom constraint in a different group than the default to make sure you have the correct DateTime $ comision object.

+4
source share

You can use the GroupSequence function for GroupSequence . In this case, the object defines a group sequence, which determines that order groups should be checked.

https://symfony.com/doc/current/validation/sequence_provider.html

The solution should look like this:

 /** * @MyValidation\TotalHours(groups={"Strict"}) * @Assert\GroupSequence({"Comision", "Strict"}) */ class Comision 

Thus, he will first check all the restrictions in the Comision group (which is the same as the Default group). Only if all restrictions in this group are valid, will the second Strict group be checked, making sure $comision->getDate() will have an instance of DateTime .

+2
source share

IIRC data converters start before validation, you can go with a data converter for your date:

https://symfony.com/doc/current/form/data_transformers.html

and then, in the data converter, check if the date is valid and if it does not generate an error.

0
source share

All Articles