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

[Form] SetData on form with default value #7141

Closed
mrok opened this issue Feb 20, 2013 · 8 comments
Closed

[Form] SetData on form with default value #7141

mrok opened this issue Feb 20, 2013 · 8 comments

Comments

@mrok
Copy link

mrok commented Feb 20, 2013

I have a form with one default value:

class GearType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('options')
            ->add('model', 'choice', array('choices' => $this->getModelChoices(), 'data' => 2));
    }

one of the requirements is form can be pre-populated by re-sellers by passing parameters in URL. It is also nice feature for potential customers to copy and paste link to email, communicators, etc.

I did it this way:

    /**
 * @Route("/car/gear")
 * @Template()
 */
public function gearAction(Request $request)
{
    $form = $this->createForm(new GearType());

    if ($request->isMethod('POST')) {
        $form->bind($request);
        if ($form->isValid()) {
            return 'is valid';
        }
    } else {
        $get = $this->getRequest()->query->all();
        if (!empty($get)) {
            $normalizer = new GetSetMethodNormalizer();
            $form->setData($normalizer->denormalize($get, new Gear())); # look here 
        }
    }

    return array('form' => $form->createView());
}

unfortunately field 'options' has always default value, instead value passed as a parameter.
I have tried to change line # look here into

        $gear =  $normalizer->denormalize($get, new Gear());
        $form = $this->createForm(new GearType(), $gear);

but no result.

Is it a bug of feature?

@stof
Copy link
Member

stof commented Feb 20, 2013

The data option wins over the data coming from the underlying object. this is expected (it has been implemented this way on purpose).
If you want to put the data from the underlying object, the default value should come from the underlying object

@mrok
Copy link
Author

mrok commented Feb 22, 2013

Great, now I know more. Thanks for explanation

@mrok mrok closed this as completed Feb 22, 2013
@dewos
Copy link

dewos commented Oct 23, 2013

I'm stuck at the same thing. So the data option wins, but for un-mapped fields (or no entity at all) it's not very intuitive to setData() for nothing. @stof, can you please argument this decision implementation? Thank you.

@stof
Copy link
Member

stof commented Oct 23, 2013

@dewos you should ask @bschussek for the argumentation as he is the one who wrote most of the Form component.

@dewos
Copy link

dewos commented Oct 23, 2013

Ok...... @bschussek? :)

@webmozart
Copy link
Contributor

See this ticket and this SO answer for an explanation.

This decision has actually been a mistake. It would be better to remove data locking and to replace it by an additional condition in the data mapper. I already suggested this in a ticket, but can't find it right now.

@stof
Copy link
Member

stof commented Oct 23, 2013

@bschussek the link to this ticket is going to here.

@dewos
Copy link

dewos commented Nov 1, 2013

Ok, but maybe you should throw an exception, then. Now setData fails silently.

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

No branches or pull requests

4 participants