From 66e21a95bbd066cf1e0c011f1ed2b7271ccdb1bd Mon Sep 17 00:00:00 2001 From: Peter Rehm Date: Sat, 17 Jan 2015 18:51:22 +0100 Subject: [PATCH 01/32] Added chapter about the locale based on the user entity --- cookbook/session/locale_sticky_session.rst | 83 ++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/cookbook/session/locale_sticky_session.rst b/cookbook/session/locale_sticky_session.rst index 2aa61eed497..90dee1954f2 100644 --- a/cookbook/session/locale_sticky_session.rst +++ b/cookbook/session/locale_sticky_session.rst @@ -106,3 +106,86 @@ method:: { $locale = $request->getLocale(); } + +Setting the locale based on the user entity +------------------------------------------- + +You might want to improve even further and want to define the locale based on +the user entity of the logged in user. However since the `LocaleListener` is called +before the `FirewallListener`, which is responsible for handling authentication and +is setting the user token into the `TokenStorage`, you have no access to the user +which is logged in. + +First lets pretend you have defined a property locale in your User Entity which you +want to be used as the locale for the given user. In order to achieve the wanted locale +configuration you can set the locale which is defined for the user to the session right +after the login. Fortunately you can hook into the login process and update your session +variable before the redirect to the first page. For this you need an event listener for the +`security.interactive_login` event. + + // src/AppBundle/EventListener/UserLocaleListener.php + namespace AppBundle\EventListener; + + use Symfony\Component\HttpFoundation\Session\Session; + use Symfony\Component\Security\Http\Event\InteractiveLoginEvent; + + /** + * Stores the locale of the user in the session after the + * login. This can be used by the LocaleListener afterwards. + */ + class UserLocaleListener + { + /** + * @var Session + */ + private $session; + public function __construct(Session $session) + { + $this->session = $session; + } + /** + * @param InteractiveLoginEvent $event + */ + public function onInteractiveLogin(InteractiveLoginEvent $event) + { + $user = $event->getAuthenticationToken()->getUser(); + $this->session->set('_locale', $user->getLocale()); + } + } + +Then register the listener: + +.. configuration-block:: + + .. code-block:: yaml + + # app/config/services.yml + services: + app.user_locale_listener: + class: AppBundle\EventListener\UserLocaleListener + tags: + - { name: kernel.event_listener, event: security.interactive_login, method: onInteractiveLogin } + + .. code-block:: xml + + + + + + + .. code-block:: php + + // app/config/services.php + $container + ->register('kernel.listener.your_listener_name', 'AppBundle\EventListener\UserLocaleListener') + ->addTag('kernel.event_listener', array('event' => 'security.interactive_login', 'method' => 'onInteractiveLogin')) + ; + +.. caution:: + +With this configuration you are all set for having the locale based on the user's +locale. If however the locale changes during the session it would not be updated +since with the current implementation the user locale will only be stored to the +session on login. In order to update the language immediately after a user has +changed his language you need to update the session variable after an update to +the user entity. From 6e986e188089fa8c19452e3d33c56ddf0aabf687 Mon Sep 17 00:00:00 2001 From: Peter Rehm Date: Sat, 17 Jan 2015 23:57:57 +0100 Subject: [PATCH 02/32] Updated wording of the chapter as per discussion --- cookbook/session/locale_sticky_session.rst | 35 +++++++++++++--------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/cookbook/session/locale_sticky_session.rst b/cookbook/session/locale_sticky_session.rst index 90dee1954f2..ade2a54957d 100644 --- a/cookbook/session/locale_sticky_session.rst +++ b/cookbook/session/locale_sticky_session.rst @@ -107,21 +107,23 @@ method:: $locale = $request->getLocale(); } -Setting the locale based on the user entity +Setting the Locale based on the User Entity ------------------------------------------- -You might want to improve even further and want to define the locale based on -the user entity of the logged in user. However since the `LocaleListener` is called -before the `FirewallListener`, which is responsible for handling authentication and -is setting the user token into the `TokenStorage`, you have no access to the user +You might want to improve this technique even further and define the locale based on +the user entity of the logged in user. However since the ``LocaleListener`` is called +before the ``FirewallListener``, which is responsible for handling authentication and +is setting the user token into the ``TokenStorage``, you have no access to the user which is logged in. -First lets pretend you have defined a property locale in your User Entity which you +First lets pretend you have defined a property locale in your user entity which you want to be used as the locale for the given user. In order to achieve the wanted locale configuration you can set the locale which is defined for the user to the session right after the login. Fortunately you can hook into the login process and update your session variable before the redirect to the first page. For this you need an event listener for the -`security.interactive_login` event. +``security.interactive_login`` event. + +.. code-block:: php // src/AppBundle/EventListener/UserLocaleListener.php namespace AppBundle\EventListener; @@ -139,17 +141,22 @@ variable before the redirect to the first page. For this you need an event liste * @var Session */ private $session; + public function __construct(Session $session) { $this->session = $session; } + /** * @param InteractiveLoginEvent $event */ public function onInteractiveLogin(InteractiveLoginEvent $event) { $user = $event->getAuthenticationToken()->getUser(); - $this->session->set('_locale', $user->getLocale()); + + if (null !== $user->getLocale()) { + $this->session->set('_locale', $user->getLocale()); + } } } @@ -183,9 +190,9 @@ Then register the listener: .. caution:: -With this configuration you are all set for having the locale based on the user's -locale. If however the locale changes during the session it would not be updated -since with the current implementation the user locale will only be stored to the -session on login. In order to update the language immediately after a user has -changed his language you need to update the session variable after an update to -the user entity. + With this configuration you are all set for having the locale based on the user's + locale. If however the locale changes during the session it would not be updated + since with the current implementation the user locale will only be stored to the + session on login. In order to update the language immediately after a user has + changed their language you need to update the session variable after an update to + the user entity. From 8be88af2f7a7a6c553a0b47fa9bce04a5ffe82b1 Mon Sep 17 00:00:00 2001 From: Alexander Schwenn Date: Thu, 8 Jan 2015 02:08:20 +0100 Subject: [PATCH 03/32] [Contributing] Several tweaks --- contributing/documentation/format.rst | 3 +- contributing/documentation/overview.rst | 33 +++++++++++++++------ contributing/documentation/standards.rst | 12 +++++--- contributing/documentation/translations.rst | 2 +- 4 files changed, 35 insertions(+), 15 deletions(-) diff --git a/contributing/documentation/format.rst b/contributing/documentation/format.rst index eeedf1e00dd..4bb8fe4bb61 100644 --- a/contributing/documentation/format.rst +++ b/contributing/documentation/format.rst @@ -11,6 +11,7 @@ reStructuredText reStructuredText is a plaintext markup syntax similar to Markdown, but much stricter with its syntax. If you are new to reStructuredText, take some time to familiarize with this format by reading the existing `Symfony documentation`_ +in raw format. If you want to learn more about this format, check out the `reStructuredText Primer`_ tutorial and the `reStructuredText Reference`_. @@ -182,7 +183,7 @@ Symfony, you should precede your description of the change with a You can also ask a question and hide the response. This is particularly [...] If you're documenting a behavior change, it may be helpful to *briefly* describe -how the behavior has changed. +how the behavior has changed: .. code-block:: rst diff --git a/contributing/documentation/overview.rst b/contributing/documentation/overview.rst index e610df1b994..f7cd0608926 100644 --- a/contributing/documentation/overview.rst +++ b/contributing/documentation/overview.rst @@ -79,19 +79,19 @@ even remove any content, but make sure that you comply with the **Step 7.** Everything is now ready to initiate a **pull request**. Go to your forked repository at ``https//github.com//symfony-docs`` -and click on the ``Pull Requests`` link located in the sidebar. +and click on the **Pull Requests** link located in the sidebar. -Then, click on the big ``New pull request`` button. As GitHub cannot guess the +Then, click on the big **New pull request** button. As GitHub cannot guess the exact changes that you want to propose, select the appropriate branches where -changes should be applied:ยบ +changes should be applied: .. image:: /images/contributing/docs-pull-request-change-base.png :align: center -In this example, the **base repository** should be ``symfony/symfony-docs`` and -the **base branch** should be the ``2.3``, which is the branch that you selected -to base your changes on. The **compare repository** should be your forked copy -of ``symfony-docs`` and the **compare branch** should be ``improve_install_chapter``, +In this example, the **base fork** should be ``symfony/symfony-docs`` and +the **base** branch should be the ``2.3``, which is the branch that you selected +to base your changes on. The **head fork** should be your forked copy +of ``symfony-docs`` and the **compare** branch should be ``improve_install_chapter``, which is the name of the branch you created and where you made your changes. .. _pull-request-format: @@ -188,7 +188,7 @@ section: # submit the changes to your forked repository $ git add xxx.rst # (optional) only if this is a new content $ git commit xxx.rst - $ git push + $ git push origin my_changes # go to GitHub and create the Pull Request # @@ -230,7 +230,7 @@ steps to contribute to the Symfony documentation, which you can use as a # add and commit your changes $ git add xxx.rst # (optional) only if this is a new content $ git commit xxx.rst - $ git push + $ git push origin my_changes # go to GitHub and create the Pull Request # @@ -248,6 +248,21 @@ steps to contribute to the Symfony documentation, which you can use as a You guessed right: after all this hard work, it's **time to celebrate again!** +Minor Changes (e.g. Typos) +-------------------------- + +You may find just a typo and want to fix it. Due to GitHub's functional +frontend, it is quite simple to create Pull Requests right in your +browser while reading the docs on symfony.com. To do this, just click +the **edit this page** button on the upper right corner. Beforehand, +please switch to the right branch as mentioned before. Now you are able +to edit the content and describe your changes within the GitHub +frontend. When your work is done, click **Propose file change** to +create a commit and, in case it is your first contribution, also your +fork. A new branch is created automatically in order to provide a base +for your Pull Request. Then fill out the form to create the Pull Request +as described above. + Frequently Asked Questions -------------------------- diff --git a/contributing/documentation/standards.rst b/contributing/documentation/standards.rst index 39d073ec0da..2a1b10e28f5 100644 --- a/contributing/documentation/standards.rst +++ b/contributing/documentation/standards.rst @@ -58,8 +58,10 @@ Code Examples * When you fold a part of a line, e.g. a variable value, put ``...`` (without comment) at the place of the fold; * Description of the folded code: (optional) - If you fold several lines: the description of the fold can be placed after the ``...`` - If you fold only part of a line: the description can be placed before the line; + + * If you fold several lines: the description of the fold can be placed after the ``...``; + * If you fold only part of a line: the description can be placed before the line; + * If useful to the reader, a PHP code example should start with the namespace declaration; * When referencing classes, be sure to show the ``use`` statements at the @@ -77,8 +79,9 @@ Configuration examples should show all supported formats using :ref:`configuration blocks `. The supported formats (and their orders) are: -* **Configuration** (including services and routing): YAML, XML, PHP -* **Validation**: YAML, Annotations, XML, PHP +* **Configuration** (including services): YAML, XML, PHP +* **Routing**: Annotations, YAML, XML, PHP +* **Validation**: Annotations, YAML, XML, PHP * **Doctrine Mapping**: Annotations, YAML, XML, PHP * **Translation**: XML, YAML, PHP @@ -151,6 +154,7 @@ English Language Standards * **Gender-neutral language**: when referencing a hypothetical person, such as *"a user with a session cookie"*, use gender-neutral pronouns (they/their/them). For example, instead of: + * he or she, use they * him or her, use them * his or her, use their diff --git a/contributing/documentation/translations.rst b/contributing/documentation/translations.rst index 54c5002ff54..f6951d228c4 100644 --- a/contributing/documentation/translations.rst +++ b/contributing/documentation/translations.rst @@ -6,7 +6,7 @@ in the translation process. .. note:: - Symfony Project officially discourages starting new translations for the + Symfony Project officially discourages from starting new translations for the documentation. As a matter of fact, there is `an ongoing discussion`_ in the community about the benefits and drawbacks of community driven translations. From 574462367324691fcde1154cf52c4448bc9e4396 Mon Sep 17 00:00:00 2001 From: "Andrew (Andrius) Marcinkevicius" Date: Tue, 20 Jan 2015 08:47:16 +0200 Subject: [PATCH 04/32] Fix typos | Q | A | ------------- | --- | Doc fix? | yes | New docs? | no | Applies to | 2.3 | Fixed tickets | --- cookbook/form/form_collections.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cookbook/form/form_collections.rst b/cookbook/form/form_collections.rst index 9793a67d357..5a51d1a6e59 100644 --- a/cookbook/form/form_collections.rst +++ b/cookbook/form/form_collections.rst @@ -246,7 +246,7 @@ great, your user can't actually add any new tags yet. .. caution:: In this entry, you embed only one collection, but you are not limited - to this. You can also embed nested collection as many level down as you + to this. You can also embed nested collection as many levels down as you like. But if you use Xdebug in your development setup, you may receive a ``Maximum function nesting level of '100' reached, aborting!`` error. This is due to the ``xdebug.max_nesting_level`` PHP setting, which defaults @@ -459,7 +459,7 @@ is added to the ``Task`` class by calling the ``addTag`` method. Before this change, they were added internally by the form by calling ``$task->getTags()->add($tag)``. That was just fine, but forcing the use of the "adder" method makes handling these new ``Tag`` objects easier (especially if you're using Doctrine, which -we talk about next!). +we will talk about next!). .. caution:: From 0fc72a8199371e4ec4a63b94ddaffdd34de5886c Mon Sep 17 00:00:00 2001 From: Peter Rehm Date: Wed, 21 Jan 2015 08:36:54 +0100 Subject: [PATCH 05/32] Updated as per feedback --- cookbook/session/locale_sticky_session.rst | 41 ++++++++++++++-------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/cookbook/session/locale_sticky_session.rst b/cookbook/session/locale_sticky_session.rst index ade2a54957d..b7e4b0d6ebc 100644 --- a/cookbook/session/locale_sticky_session.rst +++ b/cookbook/session/locale_sticky_session.rst @@ -107,20 +107,20 @@ method:: $locale = $request->getLocale(); } -Setting the Locale based on the User Entity -------------------------------------------- +Setting the Locale Based on the User's Preferences +-------------------------------------------------- You might want to improve this technique even further and define the locale based on -the user entity of the logged in user. However since the ``LocaleListener`` is called +the user entity of the logged in user. However, since the ``LocaleListener`` is called before the ``FirewallListener``, which is responsible for handling authentication and is setting the user token into the ``TokenStorage``, you have no access to the user which is logged in. -First lets pretend you have defined a property locale in your user entity which you +Let's pretend you have defined a property "locale" in your user entity which you want to be used as the locale for the given user. In order to achieve the wanted locale -configuration you can set the locale which is defined for the user to the session right -after the login. Fortunately you can hook into the login process and update your session -variable before the redirect to the first page. For this you need an event listener for the +configuration, you can set the locale which is defined for the user to the session right +after the login. Fortunately, you can hook into the login process and update the user's +session before the redirect to the first page. For this you need an event listener for the ``security.interactive_login`` event. .. code-block:: php @@ -175,24 +175,37 @@ Then register the listener: .. code-block:: xml - - - - + + + + + + + + + + + .. code-block:: php // app/config/services.php $container - ->register('kernel.listener.your_listener_name', 'AppBundle\EventListener\UserLocaleListener') + ->register('app.user_locale_listener', 'AppBundle\EventListener\UserLocaleListener') ->addTag('kernel.event_listener', array('event' => 'security.interactive_login', 'method' => 'onInteractiveLogin')) ; .. caution:: With this configuration you are all set for having the locale based on the user's - locale. If however the locale changes during the session it would not be updated + locale. If however the locale changes during the session, it would not be updated since with the current implementation the user locale will only be stored to the session on login. In order to update the language immediately after a user has - changed their language you need to update the session variable after an update to + changed their language, you need to update the session after an update to the user entity. From 82a9635dbdc61d44c70ff31431822ea962fdfa39 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Fri, 23 Jan 2015 11:33:53 +0100 Subject: [PATCH 06/32] Reworded the explanation about when a lock is released --- components/filesystem/lock_handler.rst | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/components/filesystem/lock_handler.rst b/components/filesystem/lock_handler.rst index d57889190ef..723df1c63e1 100644 --- a/components/filesystem/lock_handler.rst +++ b/components/filesystem/lock_handler.rst @@ -62,8 +62,23 @@ You can pass an optional blocking argument as the first argument to the ``lock()`` method, which defaults to ``false``. If this is set to ``true``, your PHP code will wait indefinitely until the lock is released by another process. -The resource is automatically released by PHP at the end of the script. In -addition, you can invoke the -:method:`Symfony\\Component\\Filesystem\\LockHandler::release` method to release -the lock explicitly. Once it's released, any other process can lock the -resource. +Beware that the resource lock is automatically released as soon as PHP applies the +garbage-collection process to the ``LockHandler`` object. This means that if you +refactor the first example showed in this article as follows: + +.. code-block:: php + + use Symfony\Component\Filesystem\LockHandler; + + if (!(new LockHandler('hello.lock'))->lock()) { + // the resource "hello" is already locked by another process + + return 0; + } + +Now the code won't work as expected, because PHP's garbage collection mechanism +removes the reference to the ``LockHandler`` object and thus, the lock is released +just after it's been created. + +Another alternative way to release the lock explicitly when needed is to use the +:method:`Symfony\\Component\\Filesystem\\LockHandler::release` method. From 1aaa4915440673f75d30b28291c4c47302b06d1a Mon Sep 17 00:00:00 2001 From: Peter Rehm Date: Sat, 24 Jan 2015 09:56:31 +0100 Subject: [PATCH 07/32] Updated as per feedback. --- cookbook/session/locale_sticky_session.rst | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/cookbook/session/locale_sticky_session.rst b/cookbook/session/locale_sticky_session.rst index b7e4b0d6ebc..44a323d6b80 100644 --- a/cookbook/session/locale_sticky_session.rst +++ b/cookbook/session/locale_sticky_session.rst @@ -121,7 +121,7 @@ want to be used as the locale for the given user. In order to achieve the wanted configuration, you can set the locale which is defined for the user to the session right after the login. Fortunately, you can hook into the login process and update the user's session before the redirect to the first page. For this you need an event listener for the -``security.interactive_login`` event. +``security.interactive_login`` event: .. code-block:: php @@ -170,12 +170,13 @@ Then register the listener: services: app.user_locale_listener: class: AppBundle\EventListener\UserLocaleListener + arguments: [@session] tags: - { name: kernel.event_listener, event: security.interactive_login, method: onInteractiveLogin } .. code-block:: xml - + + + @@ -198,8 +201,11 @@ Then register the listener: // app/config/services.php $container ->register('app.user_locale_listener', 'AppBundle\EventListener\UserLocaleListener') - ->addTag('kernel.event_listener', array('event' => 'security.interactive_login', 'method' => 'onInteractiveLogin')) - ; + ->addArgument('session') + ->addTag( + 'kernel.event_listener', + array('event' => 'security.interactive_login', 'method' => 'onInteractiveLogin' + ); .. caution:: From c3ef622f0239672e2ace8b41ac7eaed8cc57b7d1 Mon Sep 17 00:00:00 2001 From: "Andrew (Andrius) Marcinkevicius" Date: Sun, 1 Feb 2015 18:15:40 +0200 Subject: [PATCH 08/32] Remove usage of first person --- cookbook/form/form_collections.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cookbook/form/form_collections.rst b/cookbook/form/form_collections.rst index 5a51d1a6e59..e94f96fe7f9 100644 --- a/cookbook/form/form_collections.rst +++ b/cookbook/form/form_collections.rst @@ -459,7 +459,7 @@ is added to the ``Task`` class by calling the ``addTag`` method. Before this change, they were added internally by the form by calling ``$task->getTags()->add($tag)``. That was just fine, but forcing the use of the "adder" method makes handling these new ``Tag`` objects easier (especially if you're using Doctrine, which -we will talk about next!). +you will learn about next!). .. caution:: From 89a73200ccfdf2df154fafc4c1f7710354adb1f0 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Mon, 2 Feb 2015 16:47:06 +0100 Subject: [PATCH 09/32] Added the new documentation inside a "caution" admonition --- components/filesystem/lock_handler.rst | 30 ++++++++++++++------------ 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/components/filesystem/lock_handler.rst b/components/filesystem/lock_handler.rst index 723df1c63e1..26c423bc9f4 100644 --- a/components/filesystem/lock_handler.rst +++ b/components/filesystem/lock_handler.rst @@ -62,23 +62,25 @@ You can pass an optional blocking argument as the first argument to the ``lock()`` method, which defaults to ``false``. If this is set to ``true``, your PHP code will wait indefinitely until the lock is released by another process. -Beware that the resource lock is automatically released as soon as PHP applies the -garbage-collection process to the ``LockHandler`` object. This means that if you -refactor the first example showed in this article as follows: +.. caution:: -.. code-block:: php + Beware that the resource lock is automatically released as soon as PHP applies + the garbage-collection process to the ``LockHandler`` object. This means that + if you refactor the first example showed in this article as follows: - use Symfony\Component\Filesystem\LockHandler; + .. code-block:: php - if (!(new LockHandler('hello.lock'))->lock()) { - // the resource "hello" is already locked by another process + use Symfony\Component\Filesystem\LockHandler; - return 0; - } + if (!(new LockHandler('hello.lock'))->lock()) { + // the resource "hello" is already locked by another process + + return 0; + } -Now the code won't work as expected, because PHP's garbage collection mechanism -removes the reference to the ``LockHandler`` object and thus, the lock is released -just after it's been created. + Now the code won't work as expected, because PHP's garbage collection mechanism + removes the reference to the ``LockHandler`` object and thus, the lock is released + just after it's been created. -Another alternative way to release the lock explicitly when needed is to use the -:method:`Symfony\\Component\\Filesystem\\LockHandler::release` method. + Another alternative way to release the lock explicitly when needed is to use the + :method:`Symfony\\Component\\Filesystem\\LockHandler::release` method. From e632d49d763c737575d4c5a3957bd17222dd4fb3 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Mon, 16 Feb 2015 12:31:11 +0100 Subject: [PATCH 10/32] Fixed the errors spotted by Wouter --- components/filesystem/lock_handler.rst | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/components/filesystem/lock_handler.rst b/components/filesystem/lock_handler.rst index 26c423bc9f4..36fe49e271e 100644 --- a/components/filesystem/lock_handler.rst +++ b/components/filesystem/lock_handler.rst @@ -64,11 +64,10 @@ PHP code will wait indefinitely until the lock is released by another process. .. caution:: - Beware that the resource lock is automatically released as soon as PHP applies - the garbage-collection process to the ``LockHandler`` object. This means that - if you refactor the first example showed in this article as follows: - - .. code-block:: php + Be aware of the fact that the resource lock is automatically released as soon + as PHP applies the garbage-collection process to the ``LockHandler`` object. + This means that if you refactor the first example showed in this article as + follows:: use Symfony\Component\Filesystem\LockHandler; From c49f75d22437ef201b0870cd4c0039950e614f88 Mon Sep 17 00:00:00 2001 From: WouterJ Date: Mon, 5 Jan 2015 23:39:13 +0100 Subject: [PATCH 11/32] Made validation chapter best-practices-compatible --- book/validation.rst | 462 +++++++++++++++++++++++--------------------- 1 file changed, 242 insertions(+), 220 deletions(-) diff --git a/book/validation.rst b/book/validation.rst index b365a84b057..656a2600978 100644 --- a/book/validation.rst +++ b/book/validation.rst @@ -22,8 +22,8 @@ The best way to understand validation is to see it in action. To start, suppose you've created a plain-old-PHP object that you need to use somewhere in your application:: - // src/Acme/BlogBundle/Entity/Author.php - namespace Acme\BlogBundle\Entity; + // src/AppBundle/Entity/Author.php + namespace AppBundle\Entity; class Author { @@ -42,17 +42,9 @@ following: .. configuration-block:: - .. code-block:: yaml - - # src/Acme/BlogBundle/Resources/config/validation.yml - Acme\BlogBundle\Entity\Author: - properties: - name: - - NotBlank: ~ - .. code-block:: php-annotations - // src/Acme/BlogBundle/Entity/Author.php + // src/AppBundle/Entity/Author.php // ... use Symfony\Component\Validator\Constraints as Assert; @@ -65,15 +57,23 @@ following: public $name; } + .. code-block:: yaml + + # src/AppBundle/Resources/config/validation.yml + AppBundle\Entity\Author: + properties: + name: + - NotBlank: ~ + .. code-block:: xml - + - + @@ -82,7 +82,7 @@ following: .. code-block:: php - // src/Acme/BlogBundle/Entity/Author.php + // src/AppBundle/Entity/Author.php // ... use Symfony\Component\Validator\Mapping\ClassMetadata; @@ -119,11 +119,13 @@ returned. Take this simple example from inside a controller:: // ... use Symfony\Component\HttpFoundation\Response; - use Acme\BlogBundle\Entity\Author; + use AppBundle\Entity\Author; - public function indexAction() + // ... + public function authorAction() { $author = new Author(); + // ... do something to the $author object $validator = $this->get('validator'); @@ -133,7 +135,7 @@ returned. Take this simple example from inside a controller:: /* * Uses a __toString method on the $errors variable which is a * ConstraintViolationList object. This gives us a nice string - * for debugging + * for debugging. */ $errorsString = (string) $errors; @@ -148,7 +150,7 @@ message: .. code-block:: text - Acme\BlogBundle\Author.name: + AppBundle\Author.name: This value should not be blank If you insert a value into the ``name`` property, the happy success message @@ -161,12 +163,10 @@ will appear. you'll use validation indirectly when handling submitted form data. For more information, see the :ref:`book-validation-forms`. -You could also pass the collection of errors into a template. - -.. code-block:: php +You could also pass the collection of errors into a template:: if (count($errors) > 0) { - return $this->render('AcmeBlogBundle:Author:validate.html.twig', array( + return $this->render('Author/validation.html.twig', array( 'errors' => $errors, )); } @@ -177,7 +177,7 @@ Inside the template, you can output the list of errors exactly as needed: .. code-block:: html+jinja - {# src/Acme/BlogBundle/Resources/views/Author/validate.html.twig #} + {# app/Resources/views/Author/validation.html.twig #}

The author has the following errors