Micka Bup
Micka Bup

Reputation: 439

Required not working in my form with easyAdmin

I've got in my EasyAdmin a CollectionField which have in setEntryType my ExtraType => ->setEntryType(ExtraType::class). In my ExtraType, I choose 2 fields on 3 to get the required true and the last in false but the required doesn't work :

public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('description', TextareaType::class, [
                'required' => true
            ])
            ->add('isVisible', CheckboxType::class, [
                'label' => 'Visible',
            ])
            ->add('createdAt', DateTimeType::class, [
                'widget' => 'single_text',
                'label' => 'Date',
                'required' => true
            ])

        ;
    }

I don't know why the required does not appear in the dom

Upvotes: 2

Views: 2331

Answers (2)

Hein Huijskes
Hein Huijskes

Reputation: 21

After struggling with a similar problem this is my solution: Use ->setRequired(true) on the Field that uses this form type, then add 'required' => false to any field you do not want to be required.

For some reason EasyAdmin seems to only consider the 'required' attribute set in buildForm() when using ->setRequired(true). Without this setting, or even when using ->setRequired(false), EasyAdmin appears to ignore it. With this solution you can still achieve the desired result, at least I did.

Upvotes: 1

Micka Bup
Micka Bup

Reputation: 439

I found the answer, to get the required this is how I did :
'attr' => ['required' => true]

Upvotes: 3

Related Questions