1

Let's say that I have a Human entity with a $surname property, and I have two form types, one for creating the entity and one for searching in entities. When I put the annotation Assert/NotBlank() on the surname property, it is used for the search form too.

Is there any way how to specify which constraint annotation should a specific form use and which not?

2 Answers 2

3

Using Annotations you can also assign a Validation Group to the entity constraint like the example below (using annotations):

/**
* @Assert/NotBlank(
*     groups={"search_form"} <--- VALIDATION GROUP
* )
*/
protected $surname;

Depending by your needs you can add one or more validation groups to use a specific assert in different forms/contexts like: groups={"registration", "edit", "search"}

Then you can read the docs (links are for the latest version) to see:

Sign up to request clarification or add additional context in comments.

Comments

1

You could remove the constraint from the class and set it in the form builders, which would allow you to have different constraints for the same field:

class CreateHumanType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add(
                'surname',
                TextType::class,
                [
                    'label' => "surname",
                    'constraints' => [
                        new NotBlank(
                            [
                                'message' => "The surname is required",
                            ]
                        ),
                    ],
                ]
            );
    }
    //[...]
}

class SearchHumanType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add(
                'surname',
                TextType::class,
                [
                    'label' => "surname",
                    'required' => "false",
                ]
            );
    }
    //[...]
}

Alternatively, you could keep the constraint in the class and set the field as not mapped for the search:

class SearchHumanType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add(
                'surname',
                TextType::class,
                [
                    'label' => "surname",
                    'mapped' => false,
                    'required' => false,
                ]
            );
    }
    //[...]
}

You'd then have to handle it manually in the controller.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.