0
votes

On the form level validation in Symfony 3.3, I'm trying to figure out how to make it so that it is case insensitive. It is comparing against an array of choices.

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('title', CollectionType::class, [
            'entry_type' => ChoiceType::class,
            'allow_add' => true,
            'allow_delete' => true,
            'entry_options'  => array(
                'choices'  => array(
                                "Sample Option",
                                "Sample Option 2",
                                "Sample Option 3",
                                "Sample Option 4"
                            )
            ),
            'error_bubbling' => false
        ]);

Under entry_options in choices is the array that it is using for form validation.

What I'm looking to do is be able to pass a case insensitive value like "sample option" or "sample Option" and have that pass the form level validation.

Thanks in advance for any help on this.

Update - As Kevin mentioned, I'm interested in figuring out how/where this should be updated in the framework.

2
Convert choices and values to lowercase before comparison.svgrafov
@svgrafov It looks like this question is asking how or where would you be able to convert the values to lowercase, within the context of Symfony Forms.Kevin Brown-Silva

2 Answers

0
votes

What ended up happening here in case anyone else has a similar problem is there was difficulty getting the data transformer to transform with the CollectionType and ChoiceType.

Instead of using CollectionType, I changed it to TextType. This passed through the data transformer and was able to have the desired result.

$builder->add('title', TextType::class);
0
votes

There's another approach that's worth considering, aside from DataTransformers. Form Events. Specifically FormEvents::PRE_SUBMIT.

From the Symfony Docs:

The FormEvents::PRE_SUBMIT event is dispatched at the beginning of the Form::submit() method.

It can be used to:

  • Change data from the request, before submitting the data to the form;
  • Add or remove form fields, before submitting the data to the form.

This would effectively precede any form-level validation.