Skip to content

[2.2] [Form] Empty value missing for expanded single-choice field with required set to false #3154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
webmozart opened this issue Jan 19, 2012 · 13 comments

Comments

@webmozart
Copy link
Contributor

When creating an expanded, single-choice field that is not required

$form = $this->createFormBuilder()
    ->add('color', 'choice', array(
        'choices' => array(
            '990000' => 'Red',
            '009900' => 'Green',
        ),
        'expanded' => true,
        'required' => false,
    ))
    ->getForm()
;

an entry for the empty value should be added, but is not.

@biozshock
Copy link
Contributor

Actually you will not have a default value even if you set 'required' = true

@klaussilveira
Copy link
Contributor

What do you mean with an entry for the empty value? So far, the behavior looks OK for me, not a bug.

@stof
Copy link
Member

stof commented Apr 4, 2012

@bschussek ping

@webmozart
Copy link
Contributor Author

@klaussilveira There should be a radio button like this:

(o) Empty

where "Empty" corresponds to the value of the "empty_value" option.

@rdohms
Copy link
Contributor

rdohms commented Nov 23, 2012

So where are we with this?

if i have a entity field with expanded set to true, and required to false, even adding empty_value manually does not give me the empty ("no selection") radio. Which means if the use accidentally selects he cannot unselect or select a blank option anymore.

This is critical bug, 2.1 should at least get an option where explicitly setting empty value makes it show up.

@webmozart
Copy link
Contributor Author

This is not solved yet. I tried to solve it once and I remember that it was not easy.

If you need a quick solution, I'd hardcode an empty radio button in your Twig template.

@rdohms
Copy link
Contributor

rdohms commented Nov 24, 2012

@bschussek it seems to me that in the case of expanded = true and required = false with multiple = false the empty value should be parsed. However right now the hardcode index in the ChoiceType checks f ($options['multiple'] || $options['expanded']) { which is an invalid check.

Fixing this and adding the same empty value handling in the field definitions should get us a empty radio. Or am overlooking some core aspect of this?

@webmozart
Copy link
Contributor Author

@rdohms Yes, you are overlooking that the way that the options are generated for expanded fields works completely differently than for collapsed field. The check you are quoting is a safeguard because (even if the check is changed) it won't work anyway.

@craue
Copy link
Contributor

craue commented Apr 21, 2013

Any news on this?

@webmozart
Copy link
Contributor Author

Fixed in the referenced PR.

fabpot added a commit that referenced this issue May 6, 2013
This PR was merged into the master branch.

Discussion
----------

[Form] Added radio button for empty value to expanded single-choice fields

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #3154
| License       | MIT
| Doc PR        | symfony/symfony-docs#2605

Commits
-------

7933971 [Form] Added radio button for empty value to expanded single-choice fields
@fabpot fabpot closed this as completed May 6, 2013
@webdevilopers
Copy link

Maybe I misunderstood the usage of the radio button in my use case:

        $builder
            ->add('windowMaterialId', 'entity', array(
                'class' => 'Acme\AppBundle\Entity\WindowMaterial',
                'expanded' => true,
                'required' => true,
            ))
        :

This will render two choices with the values 0 and 1.

In this case I want only two buttons - the ones with the choices. But I don't want any choice pre-selected and I don't want an additional "Please Choose" choice.

I tried several variations with the empty_* parameters without succes.

Is this not possible or a wrong usage for a radio button - do you recommend a simple select then?
Just wondering because many forms simply have a yes/no choice with radio buttons which are no pre-selected.

@stof
Copy link
Member

stof commented Nov 21, 2014

@webdevilopers the rpe-selected choice will be set based on the data in the object bound to the form. If the property windowMaterialId in it does not contain one of your WindowMaterial, there will be no pre-selected element.

On a side note, naming the property windowMaterialId when it contains a WindowMaterial instance is weird. the property does not contain an id.

@webdevilopers
Copy link

Thanks @stof . I am refactoring a legacy form and yet I couldn't change the naming in production! ;)

Indeed when I started again on the form this morning I no longer experienced the mentioned pre-selection.

The only thing I recognized is that setting 'required' => false will add the extra None option which is an expected behaviour as far as I understood.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants