Skip to content

Commit 440e004

Browse files
committed
Merge branch '2.5'
* 2.5: Putting #pull-request-format anchor link back, which we link to in our contrib notes Re-adding one more version [#4290] Re-adding version back Fixing bad link Updating library/bundle installation docs to use the new composer require (no version) functionality [#4233][#4094] Making validateValue and validate changes Minor tweaks and a missing location thanks to xabbuh and WouterJ Fixes thanks to @xabbuh Adding a section about keeping BC in a re-usable bundle Adding details about the 2.4 API as comments added a versionadded comment to Callback.rst Update custom_contraint.rst to meet the new 2.5 api Don't use deprecated functions in Callback.rst Update Callback.rst Replace addViolationAt (deprecated) by buildViolation
2 parents 0f34bb8 + fa32606 commit 440e004

File tree

9 files changed

+160
-114
lines changed

9 files changed

+160
-114
lines changed

book/validation.rst

+17-4
Original file line numberDiff line numberDiff line change
@@ -822,9 +822,13 @@ With this configuration, there are three validation groups:
822822
fields only.
823823

824824
To tell the validator to use a specific group, pass one or more group names
825-
as the second argument to the ``validate()`` method::
825+
as the third argument to the ``validate()`` method::
826826

827-
$errors = $validator->validate($author, array('registration'));
827+
// If you're using the new 2.5 validation API (you probably are!)
828+
$errors = $validator->validate($author, null, array('registration'));
829+
830+
// If you're using the old 2.4 validation API
831+
// $errors = $validator->validate($author, array('registration'));
828832

829833
If no groups are specified, all constraints that belong in group ``Default``
830834
will be applied.
@@ -1189,10 +1193,19 @@ it looks like this::
11891193
$emailConstraint->message = 'Invalid email address';
11901194

11911195
// use the validator to validate the value
1196+
// If you're using the new 2.5 validation API (you probably are!)
1197+
$errorList = $this->get('validator')->validate(
1198+
$email,
1199+
$emailConstraint
1200+
);
1201+
1202+
// If you're using the old 2.4 validation API
1203+
/*
11921204
$errorList = $this->get('validator')->validateValue(
11931205
$email,
11941206
$emailConstraint
11951207
);
1208+
*/
11961209

11971210
if (count($errorList) == 0) {
11981211
// this IS a valid email address, do something
@@ -1206,13 +1219,13 @@ it looks like this::
12061219
// ...
12071220
}
12081221

1209-
By calling ``validateValue`` on the validator, you can pass in a raw value and
1222+
By calling ``validate`` on the validator, you can pass in a raw value and
12101223
the constraint object that you want to validate that value against. A full
12111224
list of the available constraints - as well as the full class name for each
12121225
constraint - is available in the :doc:`constraints reference </reference/constraints>`
12131226
section .
12141227

1215-
The ``validateValue`` method returns a :class:`Symfony\\Component\\Validator\\ConstraintViolationList`
1228+
The ``validate`` method returns a :class:`Symfony\\Component\\Validator\\ConstraintViolationList`
12161229
object, which acts just like an array of errors. Each error in the collection
12171230
is a :class:`Symfony\\Component\\Validator\\ConstraintViolation` object,
12181231
which holds the error message on its ``getMessage`` method.

components/dependency_injection/lazy_services.rst

+6-8
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,19 @@ the `ProxyManager bridge`_:
3030

3131
.. code-block:: bash
3232
33-
$ php composer.phar require symfony/proxy-manager-bridge:2.3.*
33+
$ php composer.phar require symfony/proxy-manager-bridge:~2.3
3434
3535
.. note::
3636

3737
If you're using the full-stack framework, the proxy manager bridge is already
38-
included but the actual proxy manager needs to be included. Therefore add
38+
included but the actual proxy manager needs to be included. So, run:
3939

40-
.. code-block:: json
40+
.. code-block:: bash
4141
42-
"require": {
43-
"ocramius/proxy-manager": "0.5.*"
44-
}
42+
$ php composer.phar require ocramius/proxy-manager:~0.5
4543
46-
to your ``composer.json``. Afterwards compile your container and check
47-
to make sure that you get a proxy for your lazy services.
44+
Afterwards compile your container and check to make sure that you get
45+
a proxy for your lazy services.
4846

4947
Configuration
5048
-------------

contributing/documentation/overview.rst

+2
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ to base your changes on. The **compare repository** should be your forked copy
9494
of ``symfony-docs`` and the **compare branch** should be ``improve_install_chapter``,
9595
which is the name of the branch you created and where you made your changes.
9696

97+
.. _pull-request-format:
98+
9799
**Step 8.** The last step is to prepare the **description** of the pull request.
98100
To ensure that your work is reviewed quickly, please add the following table
99101
at the beginning of your pull request description:

cookbook/bundles/best_practices.rst

+43-4
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ following standardized instructions in your ``README.md`` file.
219219
following command to download the latest stable version of this bundle:
220220
221221
```bash
222-
$ composer require <package-name> "~1"
222+
$ composer require <package-name>
223223
```
224224
225225
This command requires you to have Composer installed globally, as explained
@@ -254,9 +254,6 @@ following standardized instructions in your ``README.md`` file.
254254
}
255255
```
256256
257-
This template assumes that your bundle is in its ``1.x`` version. If not, change
258-
the ``"~1"`` installation version accordingly (``"~2"``, ``"~3"``, etc.)
259-
260257
Optionally, you can add more installation steps (*Step 3*, *Step 4*, etc.) to
261258
explain other required installation tasks, such as registering routes or
262259
dumping assets.
@@ -335,6 +332,48 @@ semantic configuration described in the cookbook.
335332
If you are defining services, they should also be prefixed with the bundle
336333
alias.
337334

335+
Custom Validation Constraints
336+
-----------------------------
337+
338+
Starting with Symfony 2.5, a new Validation API was introduced. In fact,
339+
there are 3 modes, which the user can configure in their project:
340+
341+
* 2.4: the original 2.4 and earlier validation API;
342+
* 2.5: the new 2.5 and later validation API;
343+
* 2.5-BC: the new 2.5 API with a backwards-compatible layer so that the
344+
2.4 API still works. This is only available in PHP 5.3.9+.
345+
346+
As a bundle author, you'll want to support *both* API's, since some users
347+
may still be using the 2.4 API. Specifically, if your bundle adds a violation
348+
directly to the :class:`Symfony\\Component\\Validator\\Context\\ExecutionContext`
349+
(e.g. like in a custom validation constraint), you'll need to check for which
350+
API is being used. The following code, would work for *all* users::
351+
352+
use Symfony\Component\Validator\ConstraintValidator;
353+
use Symfony\Component\Validator\Constraint;
354+
use Symfony\Component\Validator\Context\ExecutionContextInterface;
355+
// ...
356+
357+
class ContainsAlphanumericValidator extends ConstraintValidator
358+
{
359+
public function validate($value, Constraint $constraint)
360+
{
361+
if ($this->context instanceof ExecutionContextInterface) {
362+
// the 2.5 API
363+
$this->context->buildViolation($constraint->message)
364+
->setParameter('%string%', $value)
365+
->addViolation();
366+
);
367+
} else {
368+
// the 2.4 API
369+
$this->context->addViolation(
370+
$constraint->message,
371+
array('%string%' => $value)
372+
);
373+
}
374+
}
375+
}
376+
338377
Learn more from the Cookbook
339378
----------------------------
340379

cookbook/bundles/installation.rst

+36-59
Original file line numberDiff line numberDiff line change
@@ -5,69 +5,46 @@ How to Install 3rd Party Bundles
55
================================
66

77
Most bundles provide their own installation instructions. However, the
8-
basic steps for installing a bundle are the same.
8+
basic steps for installing a bundle are the same:
99

10-
Add Composer Dependencies
11-
-------------------------
10+
* `A) Add Composer Dependencies`_
11+
* `B) Enable the Bundle`_
12+
* `C) Configure the Bundle`_
1213

13-
In Symfony, dependencies are managed with Composer. It's a good idea to learn
14-
some basics of Composer in `their documentation`_.
14+
A) Add Composer Dependencies
15+
----------------------------
1516

16-
Before you can use Composer to install a bundle, you should look for a
17-
`Packagist`_ package of that bundle. For example, if you search for the popular
18-
`FOSUserBundle`_ you will find a package called `friendsofsymfony/user-bundle`_.
17+
Dependencies are managed with Composer, so if Composer is new to you, learn
18+
some basics in `their documentation`_. This has 2 steps:
1919

20-
.. note::
21-
22-
Packagist is the main archive for Composer. If you are searching
23-
for a bundle, the best thing you can do is check out
24-
`KnpBundles`_, it is the unofficial archive of Symfony Bundles. If
25-
a bundle contains a ``README`` file, it is displayed there and if it
26-
has a Packagist package it shows a link to the package. It's a
27-
really useful site to begin searching for bundles.
20+
1) Find out the Name of the Bundle on Packagist
21+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2822

29-
Now that you have the package name, you should determine the version
30-
you want to use. Usually different versions of a bundle correspond to
31-
a particular version of Symfony. This information should be in the ``README``
32-
file. If it isn't, you can use the version you want. If you choose an incompatible
33-
version, Composer will throw dependency errors when you try to install. If
34-
this happens, you can try a different version.
23+
The README for a bundle (e.g. `FOSUserBundle`_) usually tells you its name
24+
(e.g. ``friendsofsymfony/user-bundle``). If it doesn't, you can search for
25+
the library on the `Packagist.org`_ site.
3526

36-
Now you can add the bundle to your ``composer.json`` file and update the
37-
dependencies. You can do this manually:
38-
39-
1. **Add it to the ``composer.json`` file:**
27+
.. note::
4028

41-
.. code-block:: json
29+
Looking for bundles? Try searching at `KnpBundles.com`_: the unofficial
30+
archive of Symfony Bundles.
4231

43-
{
44-
...,
45-
"require": {
46-
...,
47-
"friendsofsymfony/user-bundle": "2.0.*@dev"
48-
}
49-
}
32+
2) Install the Bundle via Composer
33+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5034

51-
2. **Update the dependency:**
35+
Now that you know the package name, you can install it via Composer:
5236

5337
.. code-block:: bash
5438
55-
$ php composer.phar update friendsofsymfony/user-bundle
56-
57-
or update all dependencies
58-
59-
.. code-block:: bash
39+
$ php composer.phar require friendsofsymfony/user-bundle
6040
61-
$ php composer.phar update
41+
This will choose the best version for your project, add it to ``composer.json``
42+
and download the library into the ``vendor/`` directory. If you need a specific
43+
version, add a ``:`` and the version right after the library name (see
44+
`composer require`_).
6245

63-
Or you can do this in one command:
64-
65-
.. code-block:: bash
66-
67-
$ php composer.phar require friendsofsymfony/user-bundle:2.0.*@dev
68-
69-
Enable the Bundle
70-
-----------------
46+
B) Enable the Bundle
47+
--------------------
7148

7249
At this point, the bundle is installed in your Symfony project (in
7350
``vendor/friendsofsymfony/``) and the autoloader recognizes its classes.
@@ -91,13 +68,13 @@ The only thing you need to do now is register the bundle in ``AppKernel``::
9168
}
9269
}
9370

94-
Configure the Bundle
95-
--------------------
71+
C) Configure the Bundle
72+
-----------------------
9673

97-
Usually a bundle requires some configuration to be added to app's
98-
``app/config/config.yml`` file. The bundle's documentation will likely
99-
describe that configuration. But you can also get a reference of the
100-
bundle's config via the ``config:dump-reference`` command.
74+
It's pretty common for a bundle to need some additional setup or configuration
75+
in ``app/config/config.yml``. The bundle's documentation will tell you about
76+
the configuration, but you can also get a reference of the bundle's config
77+
via the ``config:dump-reference`` command.
10178

10279
For instance, in order to look the reference of the ``assetic`` config you
10380
can use this:
@@ -132,10 +109,10 @@ Other Setup
132109
-----------
133110

134111
At this point, check the ``README`` file of your brand new bundle to see
135-
what to do next.
112+
what to do next. Have fun!
136113

137114
.. _their documentation: http://getcomposer.org/doc/00-intro.md
138-
.. _Packagist: https://packagist.org
115+
.. _Packagist.org: https://packagist.org
139116
.. _FOSUserBundle: https://github.com/FriendsOfSymfony/FOSUserBundle
140-
.. _`friendsofsymfony/user-bundle`: https://packagist.org/packages/friendsofsymfony/user-bundle
141-
.. _KnpBundles: http://knpbundles.com/
117+
.. _KnpBundles.com: http://knpbundles.com/
118+
.. _`composer require`: https://getcomposer.org/doc/03-cli.md#require

cookbook/form/unit_testing.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ on other extensions. You need add those extensions to the factory object::
185185
{
186186
parent::setUp();
187187
188-
$validator = $this->getMock('\Symfony\Component\Validator\ValidatorInterface');
188+
$validator = $this->getMock('\Symfony\Component\Validator\Validator\ValidatorInterface');
189189
$validator->method('validate')->will($this->returnValue(new ConstraintViolationList()));
190190

191191
$this->factory = Forms::createFormFactoryBuilder()

cookbook/validation/custom_constraint.rst

+20-8
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,34 @@ The validator class is also simple, and only has one required method ``validate(
6565
public function validate($value, Constraint $constraint)
6666
{
6767
if (!preg_match('/^[a-zA-Za0-9]+$/', $value, $matches)) {
68+
// If you're using the new 2.5 validation API (you probably are!)
69+
$this->context->buildViolation($constraint->message)
70+
->setParameter('%string%', $value)
71+
->addViolation();
72+
);
73+
74+
// If you're using the old 2.4 validation API
75+
/*
6876
$this->context->addViolation(
6977
$constraint->message,
7078
array('%string%' => $value)
7179
);
80+
*/
7281
}
7382
}
7483
}
7584

76-
.. note::
77-
78-
The ``validate`` method does not return a value; instead, it adds violations
79-
to the validator's ``context`` property with an ``addViolation`` method
80-
call if there are validation failures. Therefore, a value could be considered
81-
as being valid if it causes no violations to be added to the context.
82-
The first parameter of the ``addViolation`` call is the error message to
83-
use for that violation.
85+
Inside ``validate``, you don't need to return a value. Instead, you add violations
86+
to the validator's ``context`` property and a value will be considered valid
87+
if it causes no violations. The ``buildViolation`` method takes the error
88+
message as its argument and returns an instance of
89+
:class:`Symfony\\Component\\Validator\\Violation\\ConstraintViolationBuilderInterface`.
90+
The ``addViolation`` method call finally adds the violation to the context.
91+
92+
.. versionadded:: 2.5
93+
The ``buildViolation`` method was added in Symfony 2.5. For usage examples
94+
with older Symfony versions, see the corresponding versions of this documentation
95+
page.
8496

8597
Using the new Validator
8698
-----------------------

cookbook/workflow/_vendor_deps.rst.inc

+3-20
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,12 @@ To upgrade your libraries to new versions, run ``php composer.phar update``.
2424

2525
.. tip::
2626

27-
If you want to add a new package to your application, modify the ``composer.json``
28-
file:
29-
30-
.. code-block:: json
31-
32-
{
33-
"require": {
34-
...
35-
"doctrine/doctrine-fixtures-bundle": "@dev"
36-
}
37-
}
38-
39-
and then execute the ``update`` command for this specific package, i.e.:
40-
41-
.. code-block:: bash
42-
43-
$ php composer.phar update doctrine/doctrine-fixtures-bundle
44-
45-
You can also combine both steps into a single command:
27+
If you want to add a new package to your application, run the composer
28+
``require`` command:
4629

4730
.. code-block:: bash
4831

49-
$ php composer.phar require doctrine/doctrine-fixtures-bundle:@dev
32+
$ php composer.phar require doctrine/doctrine-fixtures-bundle
5033

5134
To learn more about Composer, see `GetComposer.org`_:
5235

0 commit comments

Comments
 (0)