Skip to content

Commit 1a600cd

Browse files
committed
Merge branch '2.7' into 2.8
* 2.7: Updated according to comments Improved nginx config to not expose other php files [Form] fixed EntityType choice options Adding information about using the date type as usable date picker field
2 parents f817323 + 1e30de8 commit 1a600cd

File tree

3 files changed

+113
-29
lines changed

3 files changed

+113
-29
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/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) |
@@ -57,31 +53,78 @@ This field type is highly configurable, but easy to use. The most important
5753
options are ``input`` and ``widget``.
5854

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

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

6662
$builder->add('publishedAt', DateType::class, array(
67-
'input' => 'datetime',
6863
'widget' => 'choice',
6964
));
7065

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

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

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

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

86129
Field Options
87130
-------------
@@ -185,3 +228,5 @@ Field Variables
185228
+--------------+------------+----------------------------------------------------------------------+
186229
| date_pattern | ``string`` | A string with the date format to use. |
187230
+--------------+------------+----------------------------------------------------------------------+
231+
232+
.. _`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`_ |
@@ -131,7 +131,7 @@ choice_label
131131
The ``choice_label`` option was introduced in Symfony 2.7. Prior to Symfony
132132
2.7, it was called ``property`` (which has the same functionality).
133133

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

136136
This is the property that should be used for displaying the entities as text in
137137
the HTML element::
@@ -216,6 +216,40 @@ the Closure.
216216
Overridden Options
217217
------------------
218218

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

@@ -240,12 +274,8 @@ These options inherit from the :doc:`ChoiceType </reference/forms/types/choice>`
240274

241275
.. include:: /reference/forms/types/options/choice_attr.rst.inc
242276

243-
.. include:: /reference/forms/types/options/choice_name.rst.inc
244-
245277
.. include:: /reference/forms/types/options/choice_translation_domain.rst.inc
246278

247-
.. include:: /reference/forms/types/options/choice_value.rst.inc
248-
249279
.. include:: /reference/forms/types/options/expanded.rst.inc
250280

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

0 commit comments

Comments
 (0)