-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Missing an entry on setting default values in Symfony Forms #2871
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
There is already a Some other type references should include this under "Inherit options", to make it clear. I propose at least:
This can be easily done by adding a line to the table at the top of each reference: +-------------+-------------------------------------------------------------------+
| Rendered as | ``input url`` field |
+-------------+-------------------------------------------------------------------+
| Options | - `default_protocol`_ |
+-------------+-------------------------------------------------------------------+
| Inherited | - `max_length`_ |
| options | - `required`_ |
| | - `data`_ |
| | ... |
+-------------+-------------------------------------------------------------------+ And then in the "Inherit data" section, an include should be placed: Inherited Options
-----------------
These options inherit from the :doc:`form </reference/forms/types/form>` type:
.. include:: /reference/forms/types/options/data.rst.inc Related issue: #2360 |
There is another interesting answer on Stackoverflow covering a few options - does it make sense to include those in the documentation? |
I added the As for the discussions on Stack Overflow I'm not sure if we need any more information in the documentation about the default values. |
It's now complete with |
@luebbert42 which additional options do you mean? |
I am not sure we are talking about the same thing: from my experience "data" sets a value ALWAYS, not only when the form element has no value yet. Let's say you want to create a form "Add a new address" to your application. For some reason in 99% of the use cases the city is "Amsterdam". For convenience you want to preset the city atttribute to "Amsterdam", but the user can change it for the rare case his address is not in Amsterdam, but in Utrecht. . When he edits this special address later on, the form should show "Utrecht" of course, not Amsterdam. My original request was to document this case. 😄 |
@luebbert42 then you should just use 'Amsterdam' as a default of your project |
Agreed. I think the topic of default values in the fields is not tightly connected to the documentation of the form types themselves. I mean you can specify the default value in different places depending on what your form represents (is it connected to an entity, a contact form, etc.). Still @luebbert42 I don't quite understand what do you mean by "sets the value always". You create a form and the defaults will be populated with what you have in the Sticking to your example. If this is some kind of address you will be editing afterwards it means that you're persisting it. Thus you probably have some kind of |
@wouterj excactly - where in the manual do I find the information how to do that for the different form elements? |
@mtrojanowski have to try that with the current version - when I had the question 5 months ago it did not work that way OR I did it wrong (probably the latter) ;-) |
@luebbert42 it's the same for each form type, it's just using the object: class Address
{
protected $city = 'Amsterdam';
protected $country = 'Nederland';
// ... getters/setters
}
$address = new Address();
$form = $this->createFormBuilder($address)
->add('city', 'text')
->add('country', 'text')
->getForm(); Now, when rendering the form it has the defaults 'Amsterdam' and 'Nederland'. When changing this to 'Utrecht' and submitting the form (and you persist the object). The next time you generate the edit form, you use: $address = $objectManager->find(...);
$form = $this->createFormBuilder($address)
->add('city', 'text')
->add('country', 'text')
->getForm(); And now the defaults are 'Utrecht' and 'Nederland'. |
@wouterj Yes, that's an option for text field. How would you do that with elements displayed as checkbox or radiobuttons? I would have expected that setting the data attr in the form builder class handles default values correctly for all elements. This is not the case. I tried it with a fresh 2.4 project. Using the "data" attributes for default values is still not working (or I am still doing it wrong ;-)) I have uploaded a tar.gz of the complete demo app directory if you want to try it out, too: The code is all generated by the Symfony console tool but a few characters in src/Acme/DemoBundle/Form/AddressType.php Access the CRUD thingy through: |
With checkbox or radiobuttons, you don't use a string value but a boolean value: class User
{
protected $admin = false;
} |
does it make sense to have that in the manual or cookbook? |
I've now said a couple of things:
We documented (1) in the book article and (2) in the reference articles of checkbox and radiobutton. So we already documented everything :) |
I totally agree with 1. This is very well explained and easy to understand as a beginner. Which paragraph are you refering to for "2"? (sorry for being such a pain in the ass, we can also close the issue and I'll shut up :-)) |
First paragraph :)
no, the PR is not yet merged so we can't close this one :) And I think it only improves the docs when we can have discussions like this with people reading our docs |
let me summarize in return what I know about setting default values in the sense of
now in Symfony:
From my opinion this is still not obvious from the documentation. |
In the book article @wouterj mentioned it explicitly states that the form takes the values for fields from the entity passed to it. There's also this section. Which shows you how can you pass defaults to the form when using forms without a related entity class. Maybe we can have a short article somewhere (cookbook?) about the different aspects of default values and where can we set them. Or maybe a note field in the book will be sufficient. I think additional explanation is not needed here but if you believe the topic is still not clear maybe it's worth adding something. |
Hm, we are going around in circles... "In the book article @wouterj mentioned it explicitly states that the form takes the values for fields from the entity passed to it." Do we agree that defining a "data" attribute in the form class will always overwrite values passed from the entity? |
Yes :) |
I read the discussion once again carefully and I think we somehow started to talk about slightly different things, or else - the proposed PR indeed is not exactly about what @luebbert42 originally meant. So here's my view on the topic.
Although I believe that all these can be found in different parts of the form's documentation and reference, having such a separate chapter will make it easier for people to find this particular piece of information (and will make it possible to share links directly to this chapter). What do you think? |
Thank you for the summary @mtrojanowski This should not be placed in the book chapter, as that's already full of things that don't belong there :) It would either be (a) a new cookbook article ("Where are default values comming from?") or (b) a note added to the data option. I think (b) is the best option, as we already have information about this topic and we shouldn't duplicate ourselves to much by creating yet another article about this. The note will look like: .. note::
The default values for Forms are based on the values of the properties of
the entity. The ``data`` option overrides this default value. |
👍 My idea for the section in the book chapter was to have it clearly stand out from the other info ("Hey! Looking for the way to set the default values for the form? Have a look here!"), which would also allow people to directly link to the topic. The discussion on SO shows that people are still confused with this issue. I like your solution. Unless there is more people who want that as a separate chapter we can stick to that note. Maybe I would just add something about values taken from an array (defaults do not have to come from an entity). Something like: .. note::
The default values for Form fields are taken directly from the entity
or the array passed to the form (if the form is not tied to an entity).
The ``data`` option overrides this default value. |
we can also make it generic by saying "from the resource tied to the form". Btw, don't forget to indent the note content with 4 spaces 😉 |
👍 for some generic wording. Though I prefer something like |
can we add "class variables"?
|
@xabbuh 👍 @luebbert42 I'm not sure about the |
I updated #3333 please have a look. |
Thanks everyone! If you still see something incomplete (now that I've merged #3333), let me know! Cheers! |
@weaverryan Can you tell me when we Use inheritance
how to create form ? |
Hi @Raghav9888! Please use one of the support channels to ask support question: https://symfony.com/support |
Could we have an example how to set default values to a form element using form classes?
There are some answers on Stackoverflow (the question has 18.000 views!), but I am not sure about the answer quality (yet, trying to get it running ;-)):
http://stackoverflow.com/questions/7913086/how-to-set-default-value-for-form-field-in-symfony2
The text was updated successfully, but these errors were encountered: