Skip to content

Commit 6b75721

Browse files
committed
Merge branch '2.1'
2 parents 56d0081 + 2d23d72 commit 6b75721

28 files changed

+810
-125
lines changed

book/forms.rst

+39-53
Original file line numberDiff line numberDiff line change
@@ -1520,67 +1520,53 @@ Adding Validation
15201520

15211521
The only missing piece is validation. Usually, when you call ``$form->isValid()``,
15221522
the object is validated by reading the constraints that you applied to that
1523-
class. But without a class, how can you add constraints to the data of your
1524-
form?
1525-
1526-
The answer is to setup the constraints yourself, and pass them into your
1527-
form. The overall approach is covered a bit more in the :ref:`validation chapter<book-validation-raw-values>`,
1528-
but here's a short example::
1529-
1530-
// import the namespaces above your controller class
1531-
use Symfony\Component\Validator\Constraints\Email;
1532-
use Symfony\Component\Validator\Constraints\Length;
1533-
use Symfony\Component\Validator\Constraints\Collection;
1534-
1535-
$collectionConstraint = new Collection(array(
1536-
'name' => new Length(array("min" => 5)),
1537-
'email' => new Email(array('message' => 'Invalid email address')),
1538-
));
1539-
1540-
// create a form, no default values, pass in the constraint option
1541-
$form = $this->createFormBuilder(null, array(
1542-
'constraints' => $collectionConstraint,
1543-
))->add('email', 'email')
1544-
// ...
1545-
;
1523+
class. If your form is binding to an object (i.e. you're using the ``data_class``
1524+
option or passing an object to your form), this is almost always the approach
1525+
you want to use. See :doc:`/book/validation` for more details.
15461526

1547-
Now, when you call `$form->bind($request)`, the constraints setup here are run
1548-
against your form's data. If you're using a form class, override the ``setDefaultOptions()``
1549-
method to specify the option::
1527+
.. _form-option-constraints:
15501528

1551-
namespace Acme\TaskBundle\Form\Type;
1529+
But if you're not binding to an object and are instead retrieving a simple
1530+
array of your submitted data, how can you add constraints to the data of your
1531+
form?
15521532

1553-
use Symfony\Component\Form\AbstractType;
1554-
use Symfony\Component\Form\FormBuilder;
1555-
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
1556-
use Symfony\Component\Validator\Constraints\Email;
1557-
use Symfony\Component\Validator\Constraints\Length;
1558-
use Symfony\Component\Validator\Constraints\Collection;
1533+
The answer is to setup the constraints yourself, and attach them to the individual
1534+
fields. The overall approach is covered a bit more in the :ref:`validation chapter<book-validation-raw-values>`,
1535+
but here's a short example:
15591536

1560-
class ContactType extends AbstractType
1561-
{
1562-
// ...
1537+
.. versionadded:: 2.1
1538+
The ``constraints`` option, which accepts a single constraint or an array
1539+
of constraints (before 2.1, the option was called ``validation_constraint``,
1540+
and only accepted a single constraint) is new to Symfony 2.1.
1541+
1542+
.. code-block:: php
15631543
1564-
public function setDefaultOptions(OptionsResolverInterface $resolver)
1565-
{
1566-
$collectionConstraint = new Collection(array(
1567-
'name' => new Length(array("min" => 5)),
1568-
'email' => new Email(
1569-
array('message' => 'Invalid email address')
1570-
),
1571-
));
1544+
use Symfony\Component\Validator\Constraints\MinLength;
1545+
use Symfony\Component\Validator\Constraints\NotBlank;
1546+
1547+
$builder
1548+
->add('firstName', 'text', array(
1549+
'constraints' => new MinLength(3),
1550+
))
1551+
->add('lastName', 'text', array(
1552+
'constraints' => array(
1553+
new NotBlank(),
1554+
new MinLength(3),
1555+
),
1556+
))
1557+
;
15721558
1573-
$resolver->setDefaults(array(
1574-
'constraints' => $collectionConstraint
1575-
));
1576-
}
1577-
}
1559+
.. tip::
15781560

1579-
Now, you have the flexibility to create forms - with validation - that return
1580-
an array of data, instead of an object. In most cases, it's better - and
1581-
certainly more robust - to bind your form to an object. But for simple forms,
1582-
this is a great approach.
1561+
If you are using Validation Groups, you need to either reference the
1562+
``Default`` group when creating the form, or set the correct group on
1563+
the constraint you are adding.
1564+
1565+
.. code-block:: php
15831566
1567+
new NotBlank(array('groups' => array('create', 'update'))
1568+
1569+
15841570
Final Thoughts
15851571
--------------
15861572

book/page_creation.rst

+1
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,7 @@ are used by your application (including the core Symfony bundles).
604604

605605
A bundle can live *anywhere* as long as it can be autoloaded (via the
606606
autoloader configured at ``app/autoload.php``).
607+
607608
Creating a Bundle
608609
~~~~~~~~~~~~~~~~~
609610

components/console/helpers/dialoghelper.rst

+6-5
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
Dialog Helper
55
=============
66

7-
The Dialog Helper provides functions to ask the user for more information.
8-
It is included in the default helper set, which you can get
9-
by calling :method:`Symfony\\Component\\Console\\Command\\Command::getHelperSet`::
7+
The :class:`Symfony\\Component\\Console\\Helper\\DialogHelper` provides
8+
functions to ask the user for more information. It is included in the default
9+
helper set, which you can get by calling
10+
:method:`Symfony\\Component\\Console\\Command\\Command::getHelperSet`::
1011

1112
$dialog = $this->getHelperSet()->get('dialog');
1213

@@ -19,7 +20,7 @@ Asking the User for confirmation
1920
--------------------------------
2021

2122
Suppose you want to confirm an action before actually executing it. Add
22-
the following to you command::
23+
the following to your command::
2324

2425
// ...
2526
if (!$dialog->askConfirmation(
@@ -146,4 +147,4 @@ You can also ask and validate a hidden response::
146147
);
147148

148149
If you want to allow the response to be visible if it cannot be hidden for
149-
some reason, pass true as the fifth argument.
150+
some reason, pass true as the fifth argument.

components/console/helpers/formatterhelper.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ The Formatter helpers provides functions to format the output with colors.
88
You can do more advanced things with this helper than you can in
99
:ref:`components-console-coloring`.
1010

11-
The ``formatter`` helper is included in the default helper set, which you can
12-
get by calling
11+
The :class:`Symfony\\Component\\Console\\Helper\\FormatterHelper` is included
12+
in the default helper set, which you can get by calling
1313
:method:`Symfony\\Component\\Console\\Command\\Command::getHelperSet`::
1414

1515
$formatter = $this->getHelperSet()->get('formatter');

components/console/introduction.rst

+10
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ You can install the component in many different ways:
2020
* Use the official Git repository (https://github.com/symfony/Console);
2121
* :doc:`Install it via Composer</components/using_components>` (``symfony/console`` on `Packagist`_).
2222

23+
.. note::
24+
25+
Windows does not support ANSI colors by default so the Console Component detects and
26+
disables colors where Windows does not have support. However, if Windows is not
27+
configured with an ANSI driver and your console commands invoke other scripts which
28+
emit ANSI color sequences, they will be shown as raw escape characters.
29+
30+
To enable ANSI colour support for Windows, please install `ANSICON_`.
31+
2332
Creating a basic Command
2433
------------------------
2534

@@ -470,3 +479,4 @@ Learn More!
470479
* :doc:`/components/console/single_command_tool`
471480

472481
.. _Packagist: https://packagist.org/packages/symfony/console
482+
.. _ANSICON: http://adoxa.3eeweb.com/ansicon/

components/dependency_injection/introduction.rst

+35-56
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ Basic Usage
2323
-----------
2424

2525
You might have a simple class like the following ``Mailer`` that
26-
you want to make available as a service:
27-
28-
.. code-block:: php
26+
you want to make available as a service::
2927

3028
class Mailer
3129
{
@@ -39,9 +37,7 @@ you want to make available as a service:
3937
// ...
4038
}
4139

42-
You can register this in the container as a service:
43-
44-
.. code-block:: php
40+
You can register this in the container as a service::
4541

4642
use Symfony\Component\DependencyInjection\ContainerBuilder;
4743

@@ -50,9 +46,7 @@ You can register this in the container as a service:
5046

5147
An improvement to the class to make it more flexible would be to allow
5248
the container to set the ``transport`` used. If you change the class
53-
so this is passed into the constructor:
54-
55-
.. code-block:: php
49+
so this is passed into the constructor::
5650

5751
class Mailer
5852
{
@@ -66,14 +60,13 @@ so this is passed into the constructor:
6660
// ...
6761
}
6862

69-
Then you can set the choice of transport in the container:
70-
71-
.. code-block:: php
63+
Then you can set the choice of transport in the container::
7264

7365
use Symfony\Component\DependencyInjection\ContainerBuilder;
7466

7567
$container = new ContainerBuilder();
76-
$container->register('mailer', 'Mailer')
68+
$container
69+
->register('mailer', 'Mailer')
7770
->addArgument('sendmail');
7871

7972
This class is now much more flexible as you have separated the choice of
@@ -82,66 +75,56 @@ transport out of the implementation and into the container.
8275
Which mail transport you have chosen may be something other services need to
8376
know about. You can avoid having to change it in multiple places by making
8477
it a parameter in the container and then referring to this parameter for the
85-
``Mailer`` service's constructor argument:
86-
87-
88-
.. code-block:: php
78+
``Mailer`` service's constructor argument::
8979

9080
use Symfony\Component\DependencyInjection\ContainerBuilder;
9181

9282
$container = new ContainerBuilder();
9383
$container->setParameter('mailer.transport', 'sendmail');
94-
$container->register('mailer', 'Mailer')
84+
$container
85+
->register('mailer', 'Mailer')
9586
->addArgument('%mailer.transport%');
9687

9788
Now that the ``mailer`` service is in the container you can inject it as
9889
a dependency of other classes. If you have a ``NewsletterManager`` class
99-
like this:
100-
101-
.. code-block:: php
102-
103-
use Mailer;
90+
like this::
10491

10592
class NewsletterManager
10693
{
10794
private $mailer;
10895

109-
public function __construct(Mailer $mailer)
96+
public function __construct(\Mailer $mailer)
11097
{
11198
$this->mailer = $mailer;
11299
}
113100

114101
// ...
115102
}
116103

117-
Then you can register this as a service as well and pass the ``mailer`` service into it:
118-
119-
.. code-block:: php
104+
Then you can register this as a service as well and pass the ``mailer`` service into it::
120105

121106
use Symfony\Component\DependencyInjection\ContainerBuilder;
122107
use Symfony\Component\DependencyInjection\Reference;
123108

124109
$container = new ContainerBuilder();
125110

126111
$container->setParameter('mailer.transport', 'sendmail');
127-
$container->register('mailer', 'Mailer')
112+
$container
113+
->register('mailer', 'Mailer')
128114
->addArgument('%mailer.transport%');
129115

130-
$container->register('newsletter_manager', 'NewsletterManager')
131-
->addArgument(new Reference('mailer');
116+
$container
117+
->register('newsletter_manager', 'NewsletterManager')
118+
->addArgument(new Reference('mailer'));
132119

133120
If the ``NewsletterManager`` did not require the ``Mailer`` and injecting
134-
it was only optional then you could use setter injection instead:
135-
136-
.. code-block:: php
137-
138-
use Mailer;
121+
it was only optional then you could use setter injection instead::
139122

140123
class NewsletterManager
141124
{
142125
private $mailer;
143126

144-
public function setMailer(Mailer $mailer)
127+
public function setMailer(\Mailer $mailer)
145128
{
146129
$this->mailer = $mailer;
147130
}
@@ -150,26 +133,24 @@ it was only optional then you could use setter injection instead:
150133
}
151134

152135
You can now choose not to inject a ``Mailer`` into the ``NewsletterManager``.
153-
If you do want to though then the container can call the setter method:
154-
155-
.. code-block:: php
136+
If you do want to though then the container can call the setter method::
156137

157138
use Symfony\Component\DependencyInjection\ContainerBuilder;
158139
use Symfony\Component\DependencyInjection\Reference;
159140

160141
$container = new ContainerBuilder();
161142

162143
$container->setParameter('mailer.transport', 'sendmail');
163-
$container->register('mailer', 'Mailer')
144+
$container
145+
->register('mailer', 'Mailer')
164146
->addArgument('%mailer.transport%');
165147

166-
$container->register('newsletter_manager', 'NewsletterManager')
167-
->addMethodCall('setMailer', new Reference('mailer');
148+
$container
149+
->register('newsletter_manager', 'NewsletterManager')
150+
->addMethodCall('setMailer', new Reference('mailer'));
168151

169152
You could then get your ``newsletter_manager`` service from the container
170-
like this:
171-
172-
.. code-block:: php
153+
like this::
173154

174155
use Symfony\Component\DependencyInjection\ContainerBuilder;
175156
use Symfony\Component\DependencyInjection\Reference;
@@ -201,9 +182,7 @@ Setting Up the Container with Configuration Files
201182
As well as setting up the services using PHP as above you can also use configuration
202183
files. To do this you also need to install :doc:`the Config Component</components/config/introduction>`.
203184

204-
Loading an XML config file:
205-
206-
.. code-block:: php
185+
Loading an XML config file::
207186

208187
use Symfony\Component\DependencyInjection\ContainerBuilder;
209188
use Symfony\Component\Config\FileLocator;
@@ -213,9 +192,7 @@ Loading an XML config file:
213192
$loader = new XmlFileLoader($container, new FileLocator(__DIR__));
214193
$loader->load('services.xml');
215194

216-
Loading a YAML config file:
217-
218-
.. code-block:: php
195+
Loading a YAML config file::
219196

220197
use Symfony\Component\DependencyInjection\ContainerBuilder;
221198
use Symfony\Component\Config\FileLocator;
@@ -276,10 +253,12 @@ The ``newsletter_manager`` and ``mailer`` services can be set up using config fi
276253
277254
// ...
278255
$container->setParameter('mailer.transport', 'sendmail');
279-
$container->register('mailer', 'Mailer')
280-
->addArgument('%mailer.transport%');
256+
$container
257+
->register('mailer', 'Mailer')
258+
->addArgument('%mailer.transport%');
281259
282-
$container->register('newsletter_manager', 'NewsletterManager')
283-
->addMethodCall('setMailer', new Reference('mailer');
260+
$container
261+
->register('newsletter_manager', 'NewsletterManager')
262+
->addMethodCall('setMailer', new Reference('mailer'));
284263
285-
.. _Packagist: https://packagist.org/packages/symfony/dependency-injection
264+
.. _Packagist: https://packagist.org/packages/symfony/dependency-injection

0 commit comments

Comments
 (0)