-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] Use empty_data for form default values. #16530
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
Comments
Have you tried setting an instance of your entity as data for the form? That should definitely populate the defaults. I also ran into some issues with the empty_data option, but I usually accomplish everything I want from forms without that option. Give data a try. |
yes, and it works. but it's not related to the current issue ;) |
In any way the empty_data option is not designed to populate the form default. It is supposed to be the value you want when the form is submitted empty. |
Exactly, but still, the |
I know, but IMHO it should be. |
Yes, I agree the empty_data option does not seem to work. I actually ran into this yesterday myself, which is how I found this issue. I just wanted to separate the discussion of this from setting default values on the form, because I think this is unrelated. |
@lyrixx generally in these cases it's recommended setting the default value in the underlying object before binding the form (you can use arrays as underlying object). This way, there would be no need for anything special. (comment) In your particular case, you can use $resolver->setDefaults(array(
'data_class' => Foo::class,
'data' => new Foo(),
)); because you do not have an underlying object to the form instance. However it's a common mistake to think that $model = new \stdClass();
$model->name = 'foo';
$form = $this->createFormBuilder($model)
->add('name', null, ['data' => 'bar'])
->add('submit', 'submit')
->getForm(); The initial value of the I proposed some time ago, create an option for the default value of the form fields: issue #15334 |
@lyrixx My current solution for that is to create a form type extension and to add So your form type will look like this: $builder
->add('enabled', 'checkbox', array('default' => true))
->add('name', 'text', array('default' => 'greg')) ; or like this: $resolver->setDefaults(array(
'data_class' => Foo::class,
'default' => new Foo(),
)); Working in all cases. I agree that there is an option it to behave in this way, either |
@ogizanagi The part which can confuse the cookbook is when they mention that // no data is passed in, so empty_data is
// used to get the "starting data"
$form = $this->createForm(new BlogType()); Because this empty data set would be used if you submit your form only, so never will display this data set, because never will assigned to the form data when this is created. |
@yceruto : This is actually confusing, but this is not what I'm pointing in #16530 (comment). :/ |
About This is an expected behaviour IMO, this issue should be closed and maybe adressed to the doc. Status: Reviewed |
Thanks for looking into this @HeahDude! Care to submit a PR to the docs? |
@jakzal sure, I take a look. |
Try this (in your FormType file) : |
Hello.
I did not used the form component for a while, So I may be wrong (but see this doc)
First, I pushed to code to ease understatement: lyrixx/symfony-standard@d203604
Basically, I create a form without data but with a data_class.
My "Entity" class have default values, So I expected to see the default values as the form default values. But instead, my form is empty:
If I inspect the HTML, I can see this (manually indented):
First you can note the
value="1"
in the checkbox input.Then, there is no value for the name.
I tried few symfony version from v2.6.3 to 2.7.6, it's always the same things.
So I tried to use
empty_data
=> same result.So, again, I tried to lower symfony requirement => same result for symfony 2.6.3
So I finally tried symfony 2.3 => and again, same result.
So, It could be nice to use the
empty_data
option to populate the formThe text was updated successfully, but these errors were encountered: