Symfony 5 – constraints on DateType::class

I try to figured out how I can add constraint to my DateType field using form builder

<?php

use SymfonyComponentValidatorConstraintsDate;

class InscriptionType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
         $builder->add('user_name', TextType::class, [
            'label' => 'nom',
            'constraints' => [
                new Length([
                    'min' => 3,
                    'minMessage' => 'Doit comporter 2 caractères ou plus'
                ]),
                new Regex([
                    'pattern' => '/^[wéèêàäâçïöôîûüù' -]+$/',
                    'message' => 'La chaîne comporte des caractères non autorisés'
                ]),
                new NotBlank()
            ]
        ])->add('user_birthdate', DateType::class, [
            'label' => 'Date de naissance',
            'placeholder' => [
                'year' => 'Année',
                'month' => 'Mois',
                'day' => 'Jours'
            ],
            'format' => 'ddMMyyyy',
            'constraints' => [
                new Date(),
                new NotBlank()
            ]
        ])
        
    }

}

That piece of code render date with 3 select boxes.
I always get this error ‘this value should be a string’ as I choose value from boxes

How can I use constraint to ensure that date were send is valid like the other field name ?

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *