Skip to content

Commit 00aad1e

Browse files
committed
Merge branch '3.0' into 3.1
* 3.0: Updated according to comments Improved nginx config to not expose other php files Removing all instances of choices_as_values in 3.0 [Form] fixed EntityType choice options Adding information about using the date type as usable date picker field
2 parents df818a7 + 2c10e1d commit 00aad1e

File tree

8 files changed

+116
-71
lines changed

8 files changed

+116
-71
lines changed

cookbook/configuration/web_server_configuration.rst

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,12 @@ The **minimum configuration** to get your application running under Nginx is:
305305
# Remove the internal directive to allow URIs like this
306306
internal;
307307
}
308+
309+
# return 404 for all other php files not matching the front controller
310+
# this prevents access to other php files you don't want to be accessible.
311+
location ~ \.php$ {
312+
return 404;
313+
}
308314
309315
error_log /var/log/nginx/project_error.log;
310316
access_log /var/log/nginx/project_access.log;
@@ -318,14 +324,17 @@ The **minimum configuration** to get your application running under Nginx is:
318324
.. tip::
319325

320326
This executes **only** ``app.php``, ``app_dev.php`` and ``config.php`` in
321-
the web directory. All other files will be served as text. You **must**
322-
also make sure that if you *do* deploy ``app_dev.php`` or ``config.php``
323-
that these files are secured and not available to any outside user (the
324-
IP address checking code at the top of each file does this by default).
327+
the web directory. All other files ending in ".php" will be denied.
325328

326329
If you have other PHP files in your web directory that need to be executed,
327330
be sure to include them in the ``location`` block above.
328331

332+
.. caution::
333+
334+
After you deploy to production, make sure that you **cannot** access the ``app_dev.php``
335+
or ``config.php`` scripts (i.e. ``http://example.com/app_dev.php`` and ``http://example.com/config.php``).
336+
If you *can* access these, be sure to remove the ``DEV`` section from the above configuration.
337+
329338
For advanced Nginx configuration options, read the official `Nginx documentation`_.
330339

331340
.. _`Apache documentation`: http://httpd.apache.org/docs/

reference/forms/types/choice.rst

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ To use this field, you must specify *either* ``choices`` or ``choice_loader`` op
1919
| | - `choice_name`_ |
2020
| | - `choice_translation_domain`_ |
2121
| | - `choice_value`_ |
22-
| | - `choices_as_values`_ |
22+
| | - `choices_as_values`_ (deprecated) |
2323
| | - `expanded`_ |
2424
| | - `group_by`_ |
2525
| | - `multiple`_ |
@@ -167,8 +167,6 @@ is the item's label and the array value is the item's value::
167167

168168
$builder->add('inStock', ChoiceType::class, array(
169169
'choices' => array('In Stock' => true, 'Out of Stock' => false),
170-
// always include this
171-
'choices_as_values' => true,
172170
));
173171

174172
.. include:: /reference/forms/types/options/choice_attr.rst.inc
@@ -195,39 +193,8 @@ would replace the ``choices`` option.
195193
choices_as_values
196194
~~~~~~~~~~~~~~~~~
197195

198-
**type**: ``boolean`` **default**: false
199-
200-
The ``choices_as_values`` option was added to keep backward compatibility with the
201-
*old* way of handling the ``choices`` option. When set to ``false`` (or omitted),
202-
the choice keys are used as the underlying value and the choice values are shown
203-
to the user.
204-
205-
* Before 2.7 (and deprecated now)::
206-
207-
$builder->add('gender', 'choice', array(
208-
// Shows "Male" to the user, returns "m" when selected
209-
'choices' => array('m' => 'Male', 'f' => 'Female'),
210-
// before 2.7, this option didn't actually exist, but the
211-
// behavior was equivalent to setting this to false in 2.7.
212-
'choices_as_values' => false,
213-
));
214-
215-
* Since 2.7::
216-
217-
$builder->add('gender', ChoiceType::class, array(
218-
// Shows "Male" to the user, returns "m" when selected
219-
'choices' => array('Male' => 'm', 'Female' => 'f'),
220-
'choices_as_values' => true,
221-
));
222-
223-
In Symfony 3.0, the ``choices_as_values`` option doesn't exist, but the ``choice``
224-
type behaves as if it were set to true:
225-
226-
* Default for 3.0::
227-
228-
$builder->add('gender', ChoiceType::class, array(
229-
'choices' => array('Male' => 'm', 'Female' => 'f'),
230-
));
196+
This option is deprecated and you should remove it from your 3.x projects (removing
197+
it will have *no* effect). For its purpose in 2.x, see the 2.7 documentation.
231198

232199
.. include:: /reference/forms/types/options/expanded.rst.inc
233200

reference/forms/types/date.rst

Lines changed: 61 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,8 @@ DateType Field
77
A field that allows the user to modify date information via a variety of
88
different HTML elements.
99

10-
The underlying data used for this field type can be a ``DateTime`` object,
11-
a string, a timestamp or an array. As long as the `input`_ option is set
12-
correctly, the field will take care of all of the details.
13-
14-
The field can be rendered as a single text box, three text boxes (month,
15-
day and year) or three select boxes (see the `widget`_ option).
10+
This field can be rendered in a variety of different ways via the `widget`_ option
11+
and can understand a number of different input formats via the `input`_ option.
1612

1713
+----------------------+-----------------------------------------------------------------------------+
1814
| Underlying Data Type | can be ``DateTime``, string, timestamp, or array (see the ``input`` option) |
@@ -56,31 +52,78 @@ This field type is highly configurable, but easy to use. The most important
5652
options are ``input`` and ``widget``.
5753

5854
Suppose that you have a ``publishedAt`` field whose underlying date is a
59-
``DateTime`` object. The following configures the ``DateType`` type for that
60-
field as three different choice fields::
55+
``DateTime`` object. The following configures the ``date`` type for that
56+
field as **three different choice fields**::
6157

6258
use Symfony\Component\Form\Extension\Core\Type\DateType;
6359
// ...
6460

6561
$builder->add('publishedAt', DateType::class, array(
66-
'input' => 'datetime',
6762
'widget' => 'choice',
6863
));
6964

70-
The ``input`` option *must* be changed to match the type of the underlying
71-
date data. For example, if the ``publishedAt`` field's data were a unix
72-
timestamp, you'd need to set ``input`` to ``timestamp``::
65+
If your underlying date is *not* a ``DateTime`` object (e.g. it's a unix timestamp),
66+
configure the `input`_ option.
67+
68+
Rendering a single HTML5 Textbox
69+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
70+
71+
For a better user experience, you may want to render a single text field and use
72+
some kind of "date picker" to help your user fill in the right format. To do that,
73+
use the ``single_text`` widget::
7374

7475
use Symfony\Component\Form\Extension\Core\Type\DateType;
7576
// ...
7677

7778
$builder->add('publishedAt', DateType::class, array(
78-
'input' => 'timestamp',
79-
'widget' => 'choice',
79+
// render as a single text box
80+
'widget' => 'single_text',
81+
));
82+
83+
This will render as an ``input type="date"`` HTML5 field, which means that **some -
84+
but not all - browsers will add nice date picker functionality to the field**. If you
85+
want to be absolutely sure that *every* user has a consistent date picker, use an
86+
external JavaScript library.
87+
88+
For example, suppose you want to use the `Bootstrap Datepicker`_ library. First,
89+
make the following changes::
90+
91+
use Symfony\Component\Form\Extension\Core\Type\DateType;
92+
// ...
93+
94+
$builder->add('publishedAt', DateType::class, array(
95+
'widget' => 'single_text',
96+
97+
// do not render as type="date", to avoid HTML5 date pickers
98+
'html5' => false,
99+
100+
// add a class that can eb selected in JavaScript
101+
'attr' => ['class' => 'js-datepicker'],
80102
));
81103

82-
The field also supports an ``array`` and ``string`` as valid ``input`` option
83-
values.
104+
Assuming you're using jQuery, you can initialize the date picker via:
105+
106+
.. code-block:: html
107+
108+
<script>
109+
$(document).ready(function() {
110+
$('.js-datepicker').datepicker({
111+
format: 'yyyy-mm-dd'
112+
});
113+
});
114+
</script>
115+
116+
This ``format`` key tells the date picker to use the date format that Symfony expects.
117+
This can be tricky: if the date picker is misconfigured, Symfony won't understand
118+
the format and will throw a validation error. You can also configure the format
119+
that Symfony should expect via the `format`_ option.
120+
121+
.. caution::
122+
123+
The string used by a JavaScript date picker to describe its format (e.g. ``yyyy-mm-dd``)
124+
may not match the string that Symfony uses (e.g. ``yyyy-MM-dd``). This is because
125+
different libraries use different formatting rules to describe the date format.
126+
Be aware of this - it can be tricky to make the formats truly match!
84127

85128
Field Options
86129
-------------
@@ -182,3 +225,5 @@ Field Variables
182225
+--------------+------------+----------------------------------------------------------------------+
183226
| date_pattern | ``string`` | A string with the date format to use. |
184227
+--------------+------------+----------------------------------------------------------------------+
228+
229+
.. _`Bootstrap Datepicker`: https://github.com/eternicode/bootstrap-datepicker

reference/forms/types/entity.rst

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ objects from the database.
1717
| | - `em`_ |
1818
| | - `query_builder`_ |
1919
+-------------+------------------------------------------------------------------+
20-
| Overridden | - `choices`_ |
21-
| options | - `data_class`_ |
20+
| Overridden | - `choice_name`_ |
21+
| options | - `choice_value`_ |
22+
| | - `choices`_ |
23+
| | - `data_class`_ |
2224
+-------------+------------------------------------------------------------------+
2325
| Inherited | from the :doc:`ChoiceType </reference/forms/types/choice>`: |
2426
| options | |
2527
| | - `choice_attr`_ |
26-
| | - `choice_name`_ |
2728
| | - `choice_translation_domain`_ |
28-
| | - `choice_value`_ |
2929
| | - `expanded`_ |
3030
| | - `group_by`_ |
3131
| | - `multiple`_ |
@@ -126,7 +126,7 @@ Field Options
126126
choice_label
127127
~~~~~~~~~~~~
128128

129-
**type**: ``string`` or ``callable``
129+
**type**: ``string``, ``callable`` or :class:`Symfony\\Component\\PropertyAccess\\PropertyPath`
130130

131131
This is the property that should be used for displaying the entities as text in
132132
the HTML element::
@@ -208,6 +208,40 @@ the Closure.
208208
Overridden Options
209209
------------------
210210

211+
choice_name
212+
~~~~~~~~~~~
213+
214+
.. versionadded:: 2.7
215+
The ``choice_name`` option was introduced in Symfony 2.7.
216+
217+
**type**: ``string``, ``callable`` or :class:`Symfony\\Component\\PropertyAccess\\PropertyPath` **default**: id
218+
219+
By default the name of each field is the id of the entity, if it can be read
220+
from the class metadata by an internal id reader. Otherwise the process will
221+
fall back to using increasing integers.
222+
223+
choice_value
224+
~~~~~~~~~~~~
225+
226+
.. versionadded:: 2.7
227+
The ``choice_value`` option was introduced in Symfony 2.7.
228+
229+
**type**: ``string``, ``callable`` or :class:`Symfony\\Component\\PropertyAccess\\PropertyPath` **default**: id
230+
231+
As for the ``choice_name`` option, ``choice_value`` uses the id by default.
232+
It allows an optimization in the :class:``Symfony\\Bridge\\Doctrine\\Form\\ChoiceList\\Loader\\DoctrineChoiceLoader`` which will
233+
only load the ids passed as values while the form submission.
234+
It prevents all non submitted entities to be loaded from the database, even
235+
when defining the ``query_builder`` option.
236+
If it may be useful to set this option using an entity's property as string
237+
value (e.g for some API), you will gain performances by letting this option set
238+
by default.
239+
240+
.. note::
241+
242+
If the id cannot be read, for BC, the component checks if the class implements
243+
``__toString()`` and will use an incremental integer otherwise.
244+
211245
choices
212246
~~~~~~~
213247

@@ -232,12 +266,8 @@ These options inherit from the :doc:`ChoiceType </reference/forms/types/choice>`
232266

233267
.. include:: /reference/forms/types/options/choice_attr.rst.inc
234268

235-
.. include:: /reference/forms/types/options/choice_name.rst.inc
236-
237269
.. include:: /reference/forms/types/options/choice_translation_domain.rst.inc
238270

239-
.. include:: /reference/forms/types/options/choice_value.rst.inc
240-
241271
.. include:: /reference/forms/types/options/expanded.rst.inc
242272

243273
.. include:: /reference/forms/types/options/group_by.rst.inc

reference/forms/types/options/choice_attr.rst.inc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ If an array, the keys of the ``choices`` array must be used as keys::
1818
'No' => false,
1919
'Maybe' => null,
2020
),
21-
'choices_as_values' => true,
2221
'choice_attr' => function($val, $key, $index) {
2322
// adds a class like attending_yes, attending_no, etc
2423
return ['class' => 'attending_'.strtolower($key)];

reference/forms/types/options/choice_label.rst.inc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ more control::
1616
'no' => false,
1717
'maybe' => null,
1818
),
19-
'choices_as_values' => true,
2019
'choice_label' => function ($value, $key, $index) {
2120
if ($value == true) {
2221
return 'Definitely!';
@@ -48,7 +47,6 @@ If your choice values are objects, then ``choice_label`` can also be a
4847
new Status(Status::NO),
4948
new Status(Status::MAYBE),
5049
),
51-
'choices_as_values' => true,
5250
'choice_label' => 'displayName',
5351
));
5452

reference/forms/types/options/group_by.rst.inc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ Take the following example::
2222
'1 week' => new \DateTime('+1 week'),
2323
'1 month' => new \DateTime('+1 month')
2424
),
25-
'choices_as_values' => true,
2625
'group_by' => function($val, $key, $index) {
2726
if ($val <= new \DateTime('+3 days')) {
2827
return 'Soon';

reference/forms/types/options/preferred_choices.rst.inc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ you can list the most popular on top, like Bork Bork and Pirate::
1717
'Bork' => 'muppets',
1818
'Pirate' => 'arr'
1919
),
20-
'choices_as_values' => true,
2120
'preferred_choices' => array('muppets', 'arr')
2221
));
2322

@@ -34,7 +33,6 @@ be especially useful if your values are objects::
3433
'1 week' => new \DateTime('+1 week'),
3534
'1 month' => new \DateTime('+1 month')
3635
),
37-
'choices_as_values' => true,
3836
'preferred_choices' => function ($val, $key) {
3937
// prefer options within 3 days
4038
return $val <= new \DateTime('+3 days');

0 commit comments

Comments
 (0)