Skip to content

Commit

Permalink
minor #5344 [Book] Finish #4776 and #4782 (ifdattic)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.3 branch.

Discussion
----------

[Book] Finish #4776 and #4782

| Q             | A
| ------------- | ---
| Doc fix?      | yes
| New docs?     | no
| Applies to    | all
| Fixed tickets | #4776, #4782

Commits
-------

88bd859 Update form.rst and templating.rst
  • Loading branch information
wouterj committed Jun 6, 2015
2 parents e30d0e4 + 88bd859 commit 9eb96f6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
31 changes: 17 additions & 14 deletions book/forms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ from inside a controller::
// src/AppBundle/Controller/DefaultController.php
namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use AppBundle\Entity\Task;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class DefaultController extends Controller
Expand Down Expand Up @@ -542,16 +542,17 @@ This will call the static method ``determineValidationGroups()`` on the
The Form object is passed as an argument to that method (see next example).
You can also define whole logic inline by using a ``Closure``::

use Acme\AcmeBundle\Entity\Client;
use AppBundle\Entity\Client;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

// ...
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'validation_groups' => function(FormInterface $form) {
'validation_groups' => function (FormInterface $form) {
$data = $form->getData();

if (Client::TYPE_PERSON == $data->getType()) {
return array('person');
}
Expand All @@ -565,16 +566,17 @@ Using the ``validation_groups`` option overrides the default validation
group which is being used. If you want to validate the default constraints
of the entity as well you have to adjust the option as follows::

use Acme\AcmeBundle\Entity\Client;
use AppBundle\Entity\Client;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

// ...
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'validation_groups' => function(FormInterface $form) {
'validation_groups' => function (FormInterface $form) {
$data = $form->getData();

if (Client::TYPE_PERSON == $data->getType()) {
return array('Default', 'person');
}
Expand Down Expand Up @@ -1048,7 +1050,8 @@ that will house the logic for building the task form::
$builder
->add('task')
->add('dueDate', null, array('widget' => 'single_text'))
->add('save', 'submit');
->add('save', 'submit')
;
}

public function getName()
Expand Down Expand Up @@ -1123,7 +1126,8 @@ the choice is ultimately up to you.
$builder
->add('task')
->add('dueDate', null, array('mapped' => false))
->add('save', 'submit');
->add('save', 'submit')
;
}

Additionally, if there are any fields on the form that aren't included in
Expand Down Expand Up @@ -1155,7 +1159,7 @@ easy to use in your application.
# src/AppBundle/Resources/config/services.yml
services:
acme_demo.form.type.task:
app.form.type.task:
class: AppBundle\Form\Type\TaskType
tags:
- { name: form.type, alias: task }
Expand All @@ -1169,10 +1173,7 @@ easy to use in your application.
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service
id="acme_demo.form.type.task"
class="AppBundle\Form\Type\TaskType">
<service id="app.form.type.task" class="AppBundle\Form\Type\TaskType">
<tag name="form.type" alias="task" />
</service>
</services>
Expand All @@ -1183,7 +1184,7 @@ easy to use in your application.
// src/AppBundle/Resources/config/services.php
$container
->register(
'acme_demo.form.type.task',
'app.form.type.task',
'AppBundle\Form\Type\TaskType'
)
->addTag('form.type', array(
Expand Down Expand Up @@ -1476,6 +1477,7 @@ renders the form:
{# app/Resources/views/default/new.html.twig #}
{% form_theme form 'form/fields.html.twig' %}

{# or if you want to use multiple themes #}
{% form_theme form 'form/fields.html.twig' 'Form/fields2.html.twig' %}

{# ... render the form #}
Expand All @@ -1485,6 +1487,7 @@ renders the form:
<!-- app/Resources/views/default/new.html.php -->
<?php $view['form']->setTheme($form, array('form')) ?>

<!-- or if you want to use multiple themes -->
<?php $view['form']->setTheme($form, array('form', 'form2')) ?>

<!-- ... render the form -->
Expand Down Expand Up @@ -1736,7 +1739,7 @@ file:
'Form',
),
),
)
),
// ...
));
Expand Down
26 changes: 13 additions & 13 deletions book/templating.rst
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ Throughout this chapter, template examples will be shown in both Twig and PHP.
web designers everywhere.

Twig can also do things that PHP can't, such as whitespace control,
sandboxing, automatic HTML escaping, manual contextual output escaping,
sandboxing, automatic HTML escaping, manual contextual output escaping,
and the inclusion of custom functions and filters that only affect templates.
Twig contains little features that make writing templates easier and more concise.
Take the following example, which combines a loop with a logical ``if``
Expand Down Expand Up @@ -206,8 +206,8 @@ First, build a base layout file:
<div id="sidebar">
{% block sidebar %}
<ul>
<li><a href="/">Home</a></li>
<li><a href="/blog">Blog</a></li>
<li><a href="/">Home</a></li>
<li><a href="/blog">Blog</a></li>
</ul>
{% endblock %}
</div>
Expand Down Expand Up @@ -663,7 +663,7 @@ string syntax for controllers (i.e. **bundle**:**controller**:**action**):
{# ... #}
<div id="sidebar">
{{ render(controller(
'AcmeArticleBundle:Article:recentArticles',
'AppBundle:Article:recentArticles',
{ 'max': 3 }
)) }}
</div>
Expand All @@ -676,7 +676,7 @@ string syntax for controllers (i.e. **bundle**:**controller**:**action**):
<div id="sidebar">
<?php echo $view['actions']->render(
new \Symfony\Component\HttpKernel\Controller\ControllerReference(
'AcmeArticleBundle:Article:recentArticles',
'AppBundle:Article:recentArticles',
array('max' => 3)
)
) ?>
Expand Down Expand Up @@ -796,7 +796,7 @@ in your application configuration:
// app/config/config.php
$container->loadFromExtension('framework', array(
// ...
'templating' => array(
'templating' => array(
'hinclude_default_template' => array(
'hinclude.html.twig',
),
Expand All @@ -823,7 +823,7 @@ any global default template that is defined):
new ControllerReference('...'),
array(
'renderer' => 'hinclude',
'default' => 'default/content.html.twig',
'default' => 'default/content.html.twig',
)
) ?>
Expand All @@ -841,7 +841,7 @@ Or you can also specify a string to display as the default content:
new ControllerReference('...'),
array(
'renderer' => 'hinclude',
'default' => 'Loading...',
'default' => 'Loading...',
)
) ?>
Expand Down Expand Up @@ -1014,13 +1014,13 @@ but Symfony provides a more dynamic option via the ``asset`` Twig function:

<img src="{{ asset('images/logo.png') }}" alt="Symfony!" />

<link href="{{ asset('css/blog.css') }}" rel="stylesheet" type="text/css" />
<link href="{{ asset('css/blog.css') }}" rel="stylesheet" />

.. code-block:: html+php

<img src="<?php echo $view['assets']->getUrl('images/logo.png') ?>" alt="Symfony!" />

<link href="<?php echo $view['assets']->getUrl('css/blog.css') ?>" rel="stylesheet" type="text/css" />
<link href="<?php echo $view['assets']->getUrl('css/blog.css') ?>" rel="stylesheet" />

The ``asset`` function's main purpose is to make your application more portable.
If your application lives at the root of your host (e.g. http://example.com),
Expand Down Expand Up @@ -1145,7 +1145,7 @@ is by default "web").

.. code-block:: html+jinja

<link href="{{ asset('bundles/acmedemo/css/contact.css') }}" rel="stylesheet" />
<link href="{{ asset('bundles/acmedemo/css/contact.css') }}" rel="stylesheet" />

The end result is a page that includes both the ``main.css`` and ``contact.css``
stylesheets.
Expand Down Expand Up @@ -1364,7 +1364,7 @@ One common way to use inheritance is to use a three-level approach. This
method works perfectly with the three different types of templates that were just
covered:

* Create a ``app/Resources/views/base.html.twig`` file that contains the main
* Create an ``app/Resources/views/base.html.twig`` file that contains the main
layout for your application (like in the previous example). Internally, this
template is called ``base.html.twig``;

Expand Down Expand Up @@ -1455,7 +1455,7 @@ tag to the screen:

.. code-block:: html

Hello &lt;script&gt;alert(&#39;helloe&#39;)&lt;/script&gt;
Hello &lt;script&gt;alert(&#39;hello!&#39;)&lt;/script&gt;

The Twig and PHP templating systems approach the problem in different ways.
If you're using Twig, output escaping is on by default and you're protected.
Expand Down

0 comments on commit 9eb96f6

Please sign in to comment.