8000 moved some FormView methods to FormUtil where they really belongs · 77web/symfony@566511e · GitHub
[go: up one dir, main page]

Skip to content

Commit 566511e

Browse files
committed
moved some FormView methods to FormUtil where they really belongs
1 parent 1aabc5d commit 566511e

File tree

6 files changed

+73
-51
lines changed

6 files changed

+73
-51
lines changed

src/Symfony/Bridge/Twig/Extension/FormExtension.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Bridge\Twig\TokenParser\FormThemeTokenParser;
1515
use Symfony\Component\Form\FormView;
1616
use Symfony\Component\Form\Exception\FormException;
17+
use Symfony\Component\Form\Util\FormUtil;
1718

1819
/**
1920
* FormExtension extends Twig with form capabilities.
@@ -74,15 +75,27 @@ public function getTokenParsers()
7475
public function getFunctions()
7576
{
7677
return array(
77-
'form_enctype' => new \Twig_Function_Method($this, 'renderEnctype', array('is_safe' => array('html'))),
78-
'form_widget' => new \Twig_Function_Method($this, 'renderWidget', array('is_safe' => array('html'))),
79-
'form_errors' => new \Twig_Function_Method($this, 'renderErrors', array('is_safe' => array('html'))),
80-
'form_label' => new \Twig_Function_Method($this, 'renderLabel', array('is_safe' => array('html'))),
81-
'form_row' => new \Twig_Function_Method($this, 'renderRow', array('is_safe' => array('html'))),
82-
'form_rest' => new \Twig_Function_Method($this, 'renderRest', array('is_safe' => array('html'))),
78+
'form_enctype' => new \Twig_Function_Method($this, 'renderEnctype', array('is_safe' => array('html'))),
79+
'form_widget' => new \Twig_Function_Method($this, 'renderWidget', array('is_safe' => array('html'))),
80+
'form_errors' => new \Twig_Function_Method($this, 'renderErrors', array('is_safe' => array('html'))),
81+
'form_label' => new \Twig_Function_Method($this, 'renderLabel', array('is_safe' => array('html'))),
82+
'form_row' => new \Twig_Function_Method($this, 'renderRow', array('is_safe' => array('html'))),
83+
'form_rest' => new \Twig_Function_Method($this, 'renderRest', array('is_safe' => array('html'))),
84+
'_form_is_choice_group' => new \Twig_Function_Method($this, 'isChoiceGroup', array('is_safe' => array('html'))),
85+
'_form_is_choice_selected' => new \Twig_Function_Method($this, 'isChoiceSelected', array('is_safe' => array('html'))),
8386
);
8487
}
8588

89+
public function isChoiceGroup($label)
90+
{
91+
return FormUtil::isChoiceGroup($label);
92+
}
93+
94+
public function isChoiceSelected(FormView $view, $choice)
95+
{
96+
return FormUtil::isChoiceSelected($choice, $view->get('value'));
97+
}
98+
8699
/**
87100
* Renders the HTML enctype in the form tag, if necessary
88101
*

src/Symfony/Bridge/Twig/Resources/views/Form/div_layout.html.twig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,14 @@
115115
{% block options %}
116116
{% spaceless %}
117117
{% for choice, label in options %}
118-
{% if form.choiceGroup(label) %}
118+
{% if _form_is_choice_group(label) %}
119119
<optgroup label="{{ choice }}">
120120
{% for nestedChoice, nestedLabel in label %}
121-
<option value="{{ nestedChoice }}"{% if form.choiceSelected(nestedChoice) %} selected="selected"{% endif %}>{{ nestedLabel }}</option>
121+
<option value="{{ nestedChoice }}"{% if _form_is_choice_selected(form, nestedChoice) %} selected="selected"{% endif %}>{{ nestedLabel }}</option>
122122
{% endfor %}
123123
</optgroup>
124124
{% else %}
125-
<option value="{{ choice }}"{% if form.choiceSelected(choice) %} selected="selected"{% endif %}>{{ label }}</option>
125+
<option value="{{ choice }}"{% if _form_is_choice_selected(form, choice) %} selected="selected"{% endif %}>{{ label }}</option>
126126
{% endif %}
127127
{% endfor %}
128128
{% endspaceless %}

src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/choice_widget.html.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,27 @@
1515
<?php if (!$multiple && !$required): ?><option value=""><?php echo $empty_value; ?></option><?php endif; ?>
1616
<?php if (count($preferred_choices) > 0): ?>
1717
<?php foreach ($preferred_choices as $choice => $label): ?>
18-
<?php if ($form->isChoiceGroup($label)): ?>
18+
<?php if ($view['form']->isChoiceGroup($label)): ?>
1919
<optgroup label="<?php echo $view->escape($choice) ?>">
2020
<?php foreach ($label as $nestedChoice => $nestedLabel): ?>
21-
<option value="<?php echo $view->escape($nestedChoice) ?>"<?php if ($form->isChoiceSelected($nestedChoice)): ?> selected="selected"<?php endif?>><?php echo $view->escape($nestedLabel) ?></option>
21+
<option value="<?php echo $view->escape($nestedChoice) ?>"<?php if ($view['form']->isChoiceSelected($form, $nestedChoice)): ?> selected="selected"<?php endif?>><?php echo $view->escape($nestedLabel) ?></option>
2222
<?php endforeach ?>
2323
</optgroup>
2424
<?php else: ?>
25-
<option value="<?php echo $view->escape($choice) ?>"<?php if ($form->isChoiceSelected($choice)): ?> selected="selected"<?php endif?>><?php echo $view->escape($label) ?></option>
25+
<option value="<?php echo $view->escape($choice) ?>"<?php if ($view['form']->isChoiceSelected($form, $choice)): ?> selected="selected"<?php endif?>><?php echo $view->escape($label) ?></option>
2626
<?php endif ?>
2727
<?php endforeach ?>
2828
<option disabled="disabled"><?php echo $separator ?></option>
2929
<?php endif ?>
3030
<?php foreach ($choices as $choice => $label): ?>
31-
<?php if ($form->isChoiceGroup($label)): ?>
31+
<?php if ($view['form']->isChoiceGroup($label)): ?>
3232
<optgroup label="<?php echo $view->escape($choice) ?>">
3333
<?php foreach ($label as $nestedChoice => $nestedLabel): ?>
34-
<option value="<?php echo $view->escape($nestedChoice) ?>"<?php if ($form->isChoiceSelected($nestedChoice)): ?> selected="selected"<?php endif?>><?php echo $view->escape($nestedLabel) ?></option>
34+
<option value="<?php echo $view->escape($nestedChoice) ?>"<?php if ($view['form']->isChoiceSelected($form, $nestedChoice)): ?> selected="selected"<?php endif?>><?php echo $view->escape($nestedLabel) ?></option>
3535
<?php endforeach ?>
3636
</optgroup>
3737
<?php else: ?>
38-
<option value="<?php echo $view->escape($choice) ?>"<?php if ($form->isChoiceSelected($choice)): ?> selected="selected"<?php endif?>><?php echo $view->escape($label) ?></option>
38+
<option value="<?php echo $view->escape($choice) ?>"<?php if ($view['form']->isChoiceSelected($form, $choice)): ?> selected="selected"<?php endif?>><?php echo $view->escape($label) ?></option>
3939
<?php endif ?>
4040
<?php endforeach ?>
4141
</select>

src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Templating\EngineInterface;
1616
use Symfony\Component\Form\FormView;
1717
use Symfony\Component\Form\Exception\FormException;
18+
use Symfony\Component\Form\Util\FormUtil;
1819

1920
/**
2021
*
@@ -38,6 +39,16 @@ public function __construct(EngineInterface $engine)
3839
$this->varStack = new \SplObjectStorage();
3940
}
4041

42+
public function isChoiceGroup($label)
43+
{
44+
return FormUtil::isChoiceGroup($label);
45+
}
46+
47+
public function isChoiceSelected(FormView $view, $choice)
48+
{
49+
return FormUtil::isChoiceSelected($choice, $view->get('value'));
50+
}
51+
4152
/**
4253
* Renders the attributes for the current view.
4354
*

src/Symfony/Component/Form/FormView.php

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
namespace Symfony\Component\Form;
1313

14-
use Symfony\Component\Form\Util\FormUtil;
15-
1614
class FormView implements \ArrayAccess, \IteratorAggregate, \Countable
1715
{
1816
private $vars = array(
@@ -275,38 +273,6 @@ public function getIterator()
275273
return new \ArrayIterator($this->children);
276274
}
277275

278-
/**
279-
* Returns whether the given choice is a group.
280-
*
281-
* @param mixed $choice A choice
282-
*
283-
* @return Boolean Whether the choice is a group
284-
*/
285-
public function isChoiceGroup($choice)
286-
{
287-
return is_array($choice) || $choice instanceof \Traversable;
288-
}
289-
290-
/**
291-
* Returns whether the given choice is selected.
292-
*
293-
* @param mixed $choice The choice
294-
*
295-
* @return Boolean Whether the choice is selected
296-
*/
297-
public function isChoiceSelected($choice)
298-
{
299-
$choice = FormUtil::toArrayKey($choice);
300-
301-
// The value should already have been converted by value transformers,
302-
// otherwise we had to do the conversion on every call of this method
303-
if (is_array($this->vars['value'])) {
304-
return false !== array_search($choice, $this->vars['value'], true);
305-
}
306-
307-
return $choice === $this->vars['value'];
308-
}
309-
310276
/**
311277
* Implements \Countable.
312278
*

src/Symfony/Component/Form/Util/FormUtil.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
abstract class FormUtil
1515
{
16-
public static function toArrayKey($value)
16+
static public function toArrayKey($value)
1717
{
1818
if ((string) (int) $value === (string) $value) {
1919
return (int) $value;
@@ -26,8 +26,40 @@ public static function toArrayKey($value)
2626
return (string) $value;
2727
}
2828

29-
public static function toArrayKeys(array $array)
29+
static public function toArrayKeys(array $array)
3030
{
3131
return array_map(array(__CLASS__, 'toArrayKey'), $array);
3232
}
33+
34+
/**
35+
* Returns whether the given choice is a group.
36+
*
37+
* @param mixed $choice A choice
38+
*
39+
* @return Boolean Whether the choice is a group
40+
*/
41+
s BB45 tatic public function isChoiceGroup($choice)
42+
{
43+
return is_array($choice) || $choice instanceof \Traversable;
44+
}
45+
46+
/**
47+
* Returns whether the given choice is selected.
48+
*
49+
* @param mixed $choice The choice
50+
*
51+
* @return Boolean Whether the choice is selected
52+
*/
53+
static public function isChoiceSelected($choice, $value)
54+
{
55+
$choice = FormUtil::toArrayKey($choice);
56+
57+
// The value should already have been converted by value transformers,
58+
// otherwise we had to do the conversion on every call of this method
59+
if (is_array($value)) {
60+
return false !== array_search($choice, $value, true);
61+
}
62+
63+
return $choice === $value;
64+
}
3365
}

0 commit comments

Comments
 (0)
0