b2bcodext / cms-form-builder

CMS Form Builder is a flexible OroCommerce extension that allows you to easily create forms via UI
Other
8 stars 4 forks source link

Field constrains are not applied when the field is made required #6

Open jankulma-turbine opened 3 years ago

jankulma-turbine commented 3 years ago

Hello,

We added new field type:

class CmsFormFileType extends AbstractType
{

    public function getParent()
    {
        return SymfonyFileType::class;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefault(self::ALLOWED_FILE_TYPES_OPTION, []);

        $resolver->setDefault(
            'constraints',
            function (Options $options) {
                $allowedFileTypes = $options[self::ALLOWED_FILE_TYPES_OPTION];
                if (empty($allowedFileTypes)) {
                    return null; // allow all files
                }
                return $this->convertAllowedFileTypesToConstraint($allowedFileTypes);
            }
        );
    }

    ...
}

And the constraints do work, but when the field is made required too, they are no longer applied.

I was debugging a lot, could not find where is the problem, but from what I've seen, when the field is required, NotBlank constraint is added to the field, overriding other constrains (not exactly overriding, but when the field is made required, only NotBlank constraint was on the form field, and when the field is not required, other constrains are on the form field - like they should be).

I "fixed" it using another way to add constrains (https://github.com/b2bcodext/cms-form-builder/blob/release/2.0/src/B2bCode/Bundle/CmsFormBundle/Resources/doc/dev_doc.md#event) and this way everything works.

JK