Skip to content

[Form] Deprecated "cascade_validation" #12237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions UPGRADE-2.6.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,45 @@ Form
...
{% endif %}
```

* The "cascade_validation" option was deprecated. Use the "constraints"
option together with the `Valid` constraint instead. Contrary to
"cascade_validation", "constraints" must be set on the respective child forms,
not the parent form.

Before:

```php
$form = $this->createForm('form', $article, array('cascade_validation' => true))
->add('author', new AuthorType())
->getForm();
```

After:

```php
use Symfony\Component\Validator\Constraints\Valid;

$form = $this->createForm('form', $article)
->add('author', new AuthorType(), array(
'constraints' => new Valid(),
))
->getForm();
```

Alternatively, you can set the `Valid` constraint in the model itself:

```php
use Symfony\Component\Validator\Constraints as Assert;

class Article
{
/**
* @Assert\Valid
*/
private $author;
}
```

Validator
---------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@
{{ block('form_widget_simple') }}
{%- endblock email_widget -%}

{%- block range_widget -%}
{% set type = type|default('range') %}
{{- block('form_widget_simple') -}}
{%- endblock range_widget %}

{%- block button_widget -%}
{%- if label is empty -%}
{%- if label_format is not empty -%}
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/form.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@
<service id="form.type.radio" class="Symfony\Component\Form\Extension\Core\Type\RadioType">
<tag name="form.type" alias="radio" />
</service>
<service id="form.type.range" class="Symfony\Component\Form\Extension\Core\Type\RangeType">
<tag name="form.type" alias="range" />
</service>
<service id="form.type.repeated" class="Symfony\Component\Form\Extension\Core\Type\RepeatedType">
<tag name="form.type" alias="repeated" />
</service>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php echo $view['form']->block($form, 'form_widget_simple', array('type' => isset($type) ? $type : 'range'));
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@
{% endblock %}

{% block panelContent %}
{% set filter = request.query.get('state', '-1') %}
{% set filterOptions = {
'-1': '',
(constant('Symfony\\Component\\Translation\\DataCollectorTranslator::MESSAGE_DEFINED')): 'Defined messages',
(constant('Symfony\\Component\\Translation\\DataCollectorTranslator::MESSAGE_MISSING')): 'Missing messages',
(constant('Symfony\\Component\\Translation\\DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK')): 'Fallback messages',
} %}

<h2>Translation Stats</h2>
<table>
<tbody>
Expand All @@ -72,6 +80,22 @@
<th>Missing messages</th>
<td><pre>{{ collector.countMissings }}</pre></td>
</tr>
<tr>
<th>Filter</th>
<td>
<form id="filter-form" action="" method="get" style="display: inline">
<input type="hidden" name="panel" value="translation">
<select id="filter" name="state" onchange="document.getElementById('filter-form').submit(); ">
{% for key,option in filterOptions %}
<option value="{{ key }}"{{ filter == key ? ' selected' : '' }}>{{ option }}</option>
{% endfor %}
</select>
<noscript>
<input type="submit" value="refresh">
</noscript>
</form>
</td>
</tr>
</tbody>
</table>

Expand All @@ -83,7 +107,7 @@
<th>Id</th>
<th>Message Preview</th>
</tr>
{% for message in collector.messages %}
{% for message in collector.messages if message.state == filter or filter == '-1' %}
<tr>
<td><code>{{ translator.state(message) }}</code></td>
<td><code>{{ message.locale }}</code></td>
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ CHANGELOG
-----

* deprecated option "read_only" in favor of "attr['readonly']"
* added the html5 "range" FormType
* deprecated the "cascade_validation" option in favor of setting "constraints"
with the Valid constraint

2.7.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ protected function loadTypes()
new Type\PasswordType(),
new Type\PercentType(),
new Type\RadioType(),
new Type\RangeType(),
new Type\RepeatedType(),
new Type\SearchType(),
new Type\TextareaType(),
Expand Down
33 changes: 33 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/Type/RangeType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Form\Extension\Core\Type;

use Symfony\Component\Form\AbstractType;

class RangeType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function getParent()
{
return 'text';
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'range';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Form\FormInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
Expand Down Expand Up @@ -63,6 +64,20 @@ public function validate($form, Constraint $constraint)
// in the form
$constraints = $config->getOption('constraints');
foreach ($constraints as $constraint) {
// For the "Valid" constraint, validate the data in all groups
if ($constraint instanceof Valid) {
if ($validator) {
$validator->atPath('data')->validate($form->getData(), $constraint, $groups);
} else {
// 2.4 API
$this->context->validateValue($form->getData(), $constraint, 'data', $groups);
}

continue;
}

// Otherwise validate a constraint only once for the first
// matching group
foreach ($groups as $group) {
if (in_array($group, $constraint->groups)) {
if ($validator) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function configureOptions(OptionsResolver $resolver)
$resolver->setDefaults(array(
'error_mapping' => array(),
'constraints' => array(),
'cascade_validation' => false,
'cascade_validation' => false, // deprecated
'invalid_message' => 'This value is not valid.',
'invalid_message_parameters' => array(),
'allow_extra_fields' => false,
Expand Down
31 changes: 31 additions & 0 deletions src/Symfony/Component/Form/Tests/AbstractBootstrap3LayoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1597,6 +1597,37 @@ public function testRadioWithValue()
);
}

public function testRange()
{
$form = $this->factory->createNamed('name', 'range', 42, array('attr' => array('min' => 5)));

$this->assertWidgetMatchesXpath($form->createView(), array('attr' => array('class' => 'my&class')),
'/input
[@type="range"]
[@name="name"]
[@value="42"]
[@min="5"]
[@class="my&class form-control"]
'
);
}

public function testRangeWithMinMaxValues()
{
$form = $this->factory->createNamed('name', 'range', 42, array('attr' => array('min' => 5, 'max' => 57)));

$this->assertWidgetMatchesXpath($form->createView(), array('attr' => array('class' => 'my&class')),
'/input
[@type="range"]
[@name="name"]
[@value="42"]
[@min="5"]
[@max="57"]
[@class="my&class form-control"]
'
);
}

public function testTextarea()
{
$form = $this->factory->createNamed('name', 'textarea', 'foo&bar', array(
Expand Down
29 changes: 29 additions & 0 deletions src/Symfony/Component/Form/Tests/AbstractLayoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1712,6 +1712,35 @@ public function testRadioWithValue()
);
}

public function testRange()
{
$form = $this->factory->createNamed('name', 'range', 42, array('attr' => array('min' => 5)));

$this->assertWidgetMatchesXpath($form->createView(), array(),
'/input
[@type="range"]
[@name="name"]
[@value="42"]
[@min="5"]
'
);
}

public function testRangeWithMinMaxValues()
{
$form = $this->factory->createNamed('name', 'range', 42, array('attr' => array('min' => 5, 'max' => 57)));

$this->assertWidgetMatchesXpath($form->createView(), array(),
'/input
[@type="range"]
[@name="name"]
[@value="42"]
[@min="5"]
[@max="57"]
'
);
}

public function testTextarea()
{
$form = $this->factory->createNamed('name', 'textarea', 'foo&bar', array(
Expand Down
Loading