Skip to content

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

@jankulma-turbine

Description

@jankulma-turbine

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions