symfony form field’s required (*) flag ignores validation groups

I’m using entity @Assert annotations with different groups tags.

  /**
   * @var string
   * @AssertNotBlank(groups={"basic"})
   * @ORMColumn(name="name", type="string", length=255, nullable=true)
   */
  private $name = null;

  /**
   * @var string
   * @AssertNotBlank(groups={"advanced"})
   * @ORMColumn(name="full_name", type="string", length=255, nullable=true)
   */
  private $fullName = null;

So it has basic and advanced groups defined.
When I creating the form, I using basic validation groups, hoping that advanced constraints won’t be affected.

$formBasics = $this->createForm(ArtistBasicType::class, $dummyArtist, [
'validation_groups' => ['basic'],
...

  public function configureOptions(OptionsResolver $resolver) {
    $resolver->setDefaults([
        'data_class'        => Artist::class,
        // 'validation_groups' => ['basic'],
    ]);
  }

I did not setup ArtistBasicType‘s default validation_group(commented out, but also tired before) and the buildForm fields are using typeguesser:

->add('fullName', null, ['label' => 'Full name'])
->add('name', null, ['label' => 'Name'])

So I did not force any required (*) flag there either.
Twig contains the following standard code too:

{{ form_row(formBasics.name,{'attr': {'class': 'form-control'}}) }}
{{ form_row(formBasics.fullName,{'attr': {'class': 'form-control'}}) }}

And yet, both fields are required.

How can I get full name field to be optional in this case, but prepared to be mandatory if I using advanced group for validating?

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *