Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Closed
marcospassos opened this Issue · 5 comments

4 participants

@marcospassos

Hello,

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" />
    </mapped-superclass>
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name', 'text')
        ->add('displayName', 'text');
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
                '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">
...
    <div>
        <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">
    </div>
...
</div>
@webmozart
Collaborator

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

@Tobion
Collaborator

$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.

@marcospassos

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
@marcospassos

Sorry, I've closed by mistake.

@marcospassos

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

@fabpot fabpot referenced this issue from a commit
@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.

Discussion
----------

[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:

```php
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.

Commits
-------

fd77b09 [Form] Fixed ValidatorTypeGuesser to guess properties without constraints not to be required
bc8ee6f
@fabpot fabpot closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.