Symfony : embeded subform and createdAt / updatedAt : validation conflict

I have an entity form that embed a collectionType and subforms.
In order to make the required option work, I had to enable Auto_mapping

        class ClassePriceType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder
            ->add('priceform', PriceformType::class, [
                'data_class' => ClassePrice::class,
                'block_prefix' => 'mainprice_block'

            ]);
        }

        public function configureOptions(OptionsResolver $resolver)
        {
            $resolver->setDefaults([
                'data_class' => ClassePrice::class,
                'attr' => array(
                    'class' => 'fullwidth'
                )
            ]);
        }
    }

class PriceformType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
        ->add('pricecover', ChoiceType::class, [
            'label' => 'Price type',
            'placeholder' => 'Select a price option',
            'choices' => [ 'Global' => '1' ,'Step' => '2'],
            'required' => true
        ])
        ->add('rangesubform', RangesubformType::class, [
            'data_class' => ClassePrice::class,
            'block_prefix' => 'range_block'

        ])
        ->add('pricesubform', PricesubformType::class, [
            'data_class' => ClassePrice::class,
            'block_prefix' => 'price_block'
        ]);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'inherit_data' => true
        ]);
    }
}

class RangesubformType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
        ->add('rangetype', EntityType::class, [
            'class' => Ptur::class,
            'label' => 'Step type',
            'choice_translation_domain'=> true,
            'required' => true
        ])
        ->add('rangeformat', EntityType::class, [
                'class' => Format::class,
                'label' => 'Format',
                'required' => true
        ])
        ->add('rangemin', IntegerType::class, [
            'label' => 'Range min',
            'required' => true
        ]);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'inherit_data' => true,
            'attr' => array(
                'class' => 'form-horizontal'
            )
        ]);
    }
}

framework:
    validation:
        email_validation_mode: html5

        # Enables validator auto-mapping support.
        # For instance, basic validation constraints will be inferred from Doctrine's metadata.
        auto_mapping:
            AppEntity: []
        enabled: 
            true   

In my entites I also use the createdAt / updatedAt auto generation made by

use GedmoTimestampableTraitsTimestampableEntity;
use GedmoSoftDeleteableTraitsSoftDeleteableEntity;
use GedmoMappingAnnotation as Gedmo;

The problem is that with auto_mapping activation, I got a validator error when creating a new entity input : createdAt and updatedAt can not be null…

Any idea how to solve that please?

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

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