Skip to content
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

Placeholder widget not selected by default in Choice field when expanded = true #15487

Closed
wiistriker opened this issue Aug 7, 2015 · 4 comments
Labels

Comments

@wiistriker
Copy link

I have such field:

->add('foo', 'choice', [
    'required' => false,
    'expanded' => true,
    'choices' => [
        'foo' => 'Foo',
        'bar' => 'Bar'
    ],
    'placeholder' => 'Placeholder'
])

Image

Why my placeholder radio isn't selected? Is it expected behaviour? I also trying to set required => true for my field

@flobb
Copy link

flobb commented Aug 7, 2015

In your controller, when you create the form, you can add the initial data (the second argument of the createForm method).
Which initial data are you using ?

@wiistriker
Copy link
Author

@florianbelhomme empty array for this example

@xabbuh xabbuh added the Form label Aug 7, 2015
@petrulis
Copy link

Placeholder can't be selected as default value. You can create choice with empty value.

    ->add('foo', 'choice', [
        'required' => false,
        'expanded' => true,
        'choices' => [
            '' => 'Placeholder',
            'foo' => 'Foo',
            'bar' => 'Bar'
        ],
    ])

Also keep in mind that placeholder for ChoiceType will be added only if choices doesn't contain empty value.

    // Check if the choices already contain the empty value
    // Only add the placeholder option if this is not the case
    if (null !== $options['placeholder'] && 0 === count($options['choice_list']->getChoicesForValues(array('')))) {
        $placeholderView = new ChoiceView(null, '', $options['placeholder']);

        // "placeholder" is a reserved name
        $this->addSubForm($builder, 'placeholder', $placeholderView, $options);
    }

@javiereguiluz
Copy link
Member

Closing it because @PetrulisDotPro's comment showed how to solve this issue.

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

No branches or pull requests

5 participants