[Form] required is rendered even when the field is nullable=true #6645

marcospassos opened this Issue Jan 9, 2013 · 5 comments

4 participants



The documentation says:

required: The required option can be guessed based on the validation rules (i.e. is the field NotBlank or NotNull) or the Doctrine metadata (i.e. is the field nullable). This is very useful, as your client-side validation will automatically match your validation rules.

But is being ignored. I have:

    <mapped-superclass name="Acme\Bundle\ProductBundle\Entity\Option\Option">
        <field name="name" column="name" type="string" />
        <field name="displayName" column="display_name" nullable="true" />
    public function buildForm(FormBuilderInterface $builder, array $options)
        $builder->add('name', 'text')
        ->add('displayName', 'text');

    public function setDefaultOptions(OptionsResolverInterface $resolver)
                'data_class' => 'Acme2\Bundle\ProductBundle\Entity\Option\Option',

    public function getName()
        return 'appedido_product_option_builder';
$form = $factory->create('appedido_product_option_builder');

return $this->render('index.html.twig', array('form' => $form->createView()));

And this is the result form:

<div id="appedido_product_option_builder">
        <label for="appedido_product_option_builder_displayName" class="required">Display Name</label>
        <input type="text" id="appedido_product_option_builder_displayName" name="appedido_product_option_builder[displayName]" required="required">
Symfony member

This was fixed before. Could you please create a fork of symfony-standard that reproduces the problem?

Symfony member

$form = $factory->create('appedido_product_option_builder');

You must provide the entity as second argument. Otherwise there is no data upon which one can guess.


I'm setting the data_class on setDefaultOptions. Furthermore, the documentation says it is "guessed based on the validation rules", not based on data (IMO, it would not make any sense).

@marcospassos marcospassos reopened this Feb 21, 2013

Sorry, I've closed by mistake.


@bschussek Done, I've opened another issue once any attribute guessing is working anymore:

@fabpot fabpot added a commit that referenced this issue Sep 24, 2014
@fabpot fabpot bug #12004 [Form] Fixed ValidatorTypeGuesser to guess properties with…
…out constraints not to be required (webmozart)

This PR was merged into the 2.3 branch.


[Form] Fixed ValidatorTypeGuesser to guess properties without constraints not to be required

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #6645
| License       | MIT
| Doc PR        | -

Consider the following entity:

class Author
     * @Assert\NotBlank
    private $name;

    private $age;

Right now, the "required" HTML attribute is set for both fields (since the default value of the "required" option is true). IMO this is wrong.

With this fix, the ValidatorTypeGuesser guesses `false` for the "required" option unless a NotNull/NotBlank constraint is present.


fd77b09 [Form] Fixed ValidatorTypeGuesser to guess properties without constraints not to be required
@fabpot fabpot closed this Sep 24, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment