Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[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

Closed
lyrixx opened this issue Nov 12, 2015 · 14 comments
Closed

[Form] Use empty_data for form default values. #16530

lyrixx opened this issue Nov 12, 2015 · 14 comments

Comments

@lyrixx
Copy link
Member

lyrixx commented Nov 12, 2015

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:

screenshot8

If I inspect the HTML, I can see this (manually indented):

<form name="foo" method="post" action="">
    <div id="foo">
        <div>
            <label for="foo_enabled" class="required">Enabled</label>
            <input type="checkbox" id="foo_enabled" name="foo[enabled]" required="required" value="1" />
        </div>
        <div>
            <label for="foo_name" class="required">Name</label>
            <input type="text" id="foo_name" name="foo[name]" required="required" />
        </div>
        <input type="hidden" id="foo__token" name="foo[_token]" value="_Y4JKjDkfmIaaiAmtoOWDz6QQDpHLy0La2dtbzue2Q4" />
    </div>
</form>

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 form

@xabbuh xabbuh added the Form label Nov 12, 2015
@Richtermeister
Copy link
Contributor

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.

@lyrixx
Copy link
Member Author

lyrixx commented Nov 15, 2015

Have you tried setting an instance of your entity as data for the form?

yes, and it works. but it's not related to the current issue ;)

@Richtermeister
Copy link
Contributor

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.

@ogizanagi
Copy link
Contributor

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 empty_data value is not propagated to subfields on submit. So what is mentioned in the cookbook is wrong or misunderstood, because in the given example, the empty_data default values of the checkbox and text fields are respectively false and '' (an empty string), which will override the properties of the Foo instance here.

@lyrixx
Copy link
Member Author

lyrixx commented Nov 15, 2015

In any way the empty_data option is not designed to populate the form default.

I know, but IMHO it should be.

@Richtermeister
Copy link
Contributor

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.

@yceruto
Copy link
Member

yceruto commented Nov 16, 2015

@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 data option without any problem:

$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 data option works as default value:

$model = new \stdClass();
$model->name = 'foo';

$form = $this->createFormBuilder($model)
    ->add('name', null, ['data' => 'bar'])
    ->add('submit', 'submit')
    ->getForm();

The initial value of the name form field would 'bar' instead of 'foo', because the default values for form fields are taken directly from the underlying data structure (e.g. an entity or an array). The data option overrides this default value. (See doc)

I proposed some time ago, create an option for the default value of the form fields: issue #15334

@yceruto
Copy link
Member

yceruto commented Nov 16, 2015

@lyrixx My current solution for that is to create a form type extension and to add default option for all form fields using FormEvents::PRE_SET_DATA: See example

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 empty_data or default

@yceruto
Copy link
Member

yceruto commented Nov 16, 2015

@ogizanagi The part which can confuse the cookbook is when they mention that empty_data is used to get the "starting data":

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

@ogizanagi
Copy link
Contributor

@yceruto : This is actually confusing, but this is not what I'm pointing in #16530 (comment). :/

@HeahDude
Copy link
Contributor

data option replaces the default null for populating field, so it's mapped to the view.
empty_data is only mapped if there was no submitted data so it cannot reach the view.

About value="1", this is just the default in CheckboxType see here.

This is an expected behaviour IMO, this issue should be closed and maybe adressed to the doc.

Status: Reviewed

@jakzal
Copy link
Contributor

jakzal commented Feb 11, 2016

Thanks for looking into this @HeahDude! Care to submit a PR to the docs?

@jakzal jakzal closed this as completed Feb 11, 2016
@HeahDude
Copy link
Contributor

@jakzal sure, I take a look.

@aheitzm2
Copy link

aheitzm2 commented May 2, 2020

Try this (in your FormType file) :
->add('myField', NumberType::class,[ 'empty_data'=>1, 'attr'=>[ 'value'=>1 ] ])

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

10 participants