moved some FormView methods to FormUtil where they really belongs
This commit is contained in:
parent
1aabc5da64
commit
566511e9e7
@ -14,6 +14,7 @@ namespace Symfony\Bridge\Twig\Extension;
|
||||
use Symfony\Bridge\Twig\TokenParser\FormThemeTokenParser;
|
||||
use Symfony\Component\Form\FormView;
|
||||
use Symfony\Component\Form\Exception\FormException;
|
||||
use Symfony\Component\Form\Util\FormUtil;
|
||||
|
||||
/**
|
||||
* FormExtension extends Twig with form capabilities.
|
||||
@ -74,15 +75,27 @@ class FormExtension extends \Twig_Extension
|
||||
public function getFunctions()
|
||||
{
|
||||
return array(
|
||||
'form_enctype' => new \Twig_Function_Method($this, 'renderEnctype', array('is_safe' => array('html'))),
|
||||
'form_widget' => new \Twig_Function_Method($this, 'renderWidget', array('is_safe' => array('html'))),
|
||||
'form_errors' => new \Twig_Function_Method($this, 'renderErrors', array('is_safe' => array('html'))),
|
||||
'form_label' => new \Twig_Function_Method($this, 'renderLabel', array('is_safe' => array('html'))),
|
||||
'form_row' => new \Twig_Function_Method($this, 'renderRow', array('is_safe' => array('html'))),
|
||||
'form_rest' => new \Twig_Function_Method($this, 'renderRest', array('is_safe' => array('html'))),
|
||||
'form_enctype' => new \Twig_Function_Method($this, 'renderEnctype', array('is_safe' => array('html'))),
|
||||
'form_widget' => new \Twig_Function_Method($this, 'renderWidget', array('is_safe' => array('html'))),
|
||||
'form_errors' => new \Twig_Function_Method($this, 'renderErrors', array('is_safe' => array('html'))),
|
||||
'form_label' => new \Twig_Function_Method($this, 'renderLabel', array('is_safe' => array('html'))),
|
||||
'form_row' => new \Twig_Function_Method($this, 'renderRow', array('is_safe' => array('html'))),
|
||||
'form_rest' => new \Twig_Function_Method($this, 'renderRest', array('is_safe' => array('html'))),
|
||||
'_form_is_choice_group' => new \Twig_Function_Method($this, 'isChoiceGroup', array('is_safe' => array('html'))),
|
||||
'_form_is_choice_selected' => new \Twig_Function_Method($this, 'isChoiceSelected', array('is_safe' => array('html'))),
|
||||
);
|
||||
}
|
||||
|
||||
public function isChoiceGroup($label)
|
||||
{
|
||||
return FormUtil::isChoiceGroup($label);
|
||||
}
|
||||
|
||||
public function isChoiceSelected(FormView $view, $choice)
|
||||
{
|
||||
return FormUtil::isChoiceSelected($choice, $view->get('value'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the HTML enctype in the form tag, if necessary
|
||||
*
|
||||
|
@ -115,14 +115,14 @@
|
||||
{% block options %}
|
||||
{% spaceless %}
|
||||
{% for choice, label in options %}
|
||||
{% if form.choiceGroup(label) %}
|
||||
{% if _form_is_choice_group(label) %}
|
||||
<optgroup label="{{ choice }}">
|
||||
{% for nestedChoice, nestedLabel in label %}
|
||||
<option value="{{ nestedChoice }}"{% if form.choiceSelected(nestedChoice) %} selected="selected"{% endif %}>{{ nestedLabel }}</option>
|
||||
<option value="{{ nestedChoice }}"{% if _form_is_choice_selected(form, nestedChoice) %} selected="selected"{% endif %}>{{ nestedLabel }}</option>
|
||||
{% endfor %}
|
||||
</optgroup>
|
||||
{% else %}
|
||||
<option value="{{ choice }}"{% if form.choiceSelected(choice) %} selected="selected"{% endif %}>{{ label }}</option>
|
||||
<option value="{{ choice }}"{% if _form_is_choice_selected(form, choice) %} selected="selected"{% endif %}>{{ label }}</option>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
{% endspaceless %}
|
||||
|
@ -15,27 +15,27 @@
|
||||
<?php if (!$multiple && !$required): ?><option value=""><?php echo $empty_value; ?></option><?php endif; ?>
|
||||
<?php if (count($preferred_choices) > 0): ?>
|
||||
<?php foreach ($preferred_choices as $choice => $label): ?>
|
||||
<?php if ($form->isChoiceGroup($label)): ?>
|
||||
<?php if ($view['form']->isChoiceGroup($label)): ?>
|
||||
<optgroup label="<?php echo $view->escape($choice) ?>">
|
||||
<?php foreach ($label as $nestedChoice => $nestedLabel): ?>
|
||||
<option value="<?php echo $view->escape($nestedChoice) ?>"<?php if ($form->isChoiceSelected($nestedChoice)): ?> selected="selected"<?php endif?>><?php echo $view->escape($nestedLabel) ?></option>
|
||||
<option value="<?php echo $view->escape($nestedChoice) ?>"<?php if ($view['form']->isChoiceSelected($form, $nestedChoice)): ?> selected="selected"<?php endif?>><?php echo $view->escape($nestedLabel) ?></option>
|
||||
<?php endforeach ?>
|
||||
</optgroup>
|
||||
<?php else: ?>
|
||||
<option value="<?php echo $view->escape($choice) ?>"<?php if ($form->isChoiceSelected($choice)): ?> selected="selected"<?php endif?>><?php echo $view->escape($label) ?></option>
|
||||
<option value="<?php echo $view->escape($choice) ?>"<?php if ($view['form']->isChoiceSelected($form, $choice)): ?> selected="selected"<?php endif?>><?php echo $view->escape($label) ?></option>
|
||||
<?php endif ?>
|
||||
<?php endforeach ?>
|
||||
<option disabled="disabled"><?php echo $separator ?></option>
|
||||
<?php endif ?>
|
||||
<?php foreach ($choices as $choice => $label): ?>
|
||||
<?php if ($form->isChoiceGroup($label)): ?>
|
||||
<?php if ($view['form']->isChoiceGroup($label)): ?>
|
||||
<optgroup label="<?php echo $view->escape($choice) ?>">
|
||||
<?php foreach ($label as $nestedChoice => $nestedLabel): ?>
|
||||
<option value="<?php echo $view->escape($nestedChoice) ?>"<?php if ($form->isChoiceSelected($nestedChoice)): ?> selected="selected"<?php endif?>><?php echo $view->escape($nestedLabel) ?></option>
|
||||
<option value="<?php echo $view->escape($nestedChoice) ?>"<?php if ($view['form']->isChoiceSelected($form, $nestedChoice)): ?> selected="selected"<?php endif?>><?php echo $view->escape($nestedLabel) ?></option>
|
||||
<?php endforeach ?>
|
||||
</optgroup>
|
||||
<?php else: ?>
|
||||
<option value="<?php echo $view->escape($choice) ?>"<?php if ($form->isChoiceSelected($choice)): ?> selected="selected"<?php endif?>><?php echo $view->escape($label) ?></option>
|
||||
<option value="<?php echo $view->escape($choice) ?>"<?php if ($view['form']->isChoiceSelected($form, $choice)): ?> selected="selected"<?php endif?>><?php echo $view->escape($label) ?></option>
|
||||
<?php endif ?>
|
||||
<?php endforeach ?>
|
||||
</select>
|
||||
|
@ -15,6 +15,7 @@ use Symfony\Component\Templating\Helper\Helper;
|
||||
use Symfony\Component\Templating\EngineInterface;
|
||||
use Symfony\Component\Form\FormView;
|
||||
use Symfony\Component\Form\Exception\FormException;
|
||||
use Symfony\Component\Form\Util\FormUtil;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -38,6 +39,16 @@ class FormHelper extends Helper
|
||||
$this->varStack = new \SplObjectStorage();
|
||||
}
|
||||
|
||||
public function isChoiceGroup($label)
|
||||
{
|
||||
return FormUtil::isChoiceGroup($label);
|
||||
}
|
||||
|
||||
public function isChoiceSelected(FormView $view, $choice)
|
||||
{
|
||||
return FormUtil::isChoiceSelected($choice, $view->get('value'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the attributes for the current view.
|
||||
*
|
||||
|
@ -11,8 +11,6 @@
|
||||
|
||||
namespace Symfony\Component\Form;
|
||||
|
||||
use Symfony\Component\Form\Util\FormUtil;
|
||||
|
||||
class FormView implements \ArrayAccess, \IteratorAggregate, \Countable
|
||||
{
|
||||
private $vars = array(
|
||||
@ -275,38 +273,6 @@ class FormView implements \ArrayAccess, \IteratorAggregate, \Countable
|
||||
return new \ArrayIterator($this->children);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the given choice is a group.
|
||||
*
|
||||
* @param mixed $choice A choice
|
||||
*
|
||||
* @return Boolean Whether the choice is a group
|
||||
*/
|
||||
public function isChoiceGroup($choice)
|
||||
{
|
||||
return is_array($choice) || $choice instanceof \Traversable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the given choice is selected.
|
||||
*
|
||||
* @param mixed $choice The choice
|
||||
*
|
||||
* @return Boolean Whether the choice is selected
|
||||
*/
|
||||
public function isChoiceSelected($choice)
|
||||
{
|
||||
$choice = FormUtil::toArrayKey($choice);
|
||||
|
||||
// The value should already have been converted by value transformers,
|
||||
// otherwise we had to do the conversion on every call of this method
|
||||
if (is_array($this->vars['value'])) {
|
||||
return false !== array_search($choice, $this->vars['value'], true);
|
||||
}
|
||||
|
||||
return $choice === $this->vars['value'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements \Countable.
|
||||
*
|
||||
|
@ -13,7 +13,7 @@ namespace Symfony\Component\Form\Util;
|
||||
|
||||
abstract class FormUtil
|
||||
{
|
||||
public static function toArrayKey($value)
|
||||
static public function toArrayKey($value)
|
||||
{
|
||||
if ((string) (int) $value === (string) $value) {
|
||||
return (int) $value;
|
||||
@ -26,8 +26,40 @@ abstract class FormUtil
|
||||
return (string) $value;
|
||||
}
|
||||
|
||||
public static function toArrayKeys(array $array)
|
||||
static public function toArrayKeys(array $array)
|
||||
{
|
||||
return array_map(array(__CLASS__, 'toArrayKey'), $array);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the given choice is a group.
|
||||
*
|
||||
* @param mixed $choice A choice
|
||||
*
|
||||
* @return Boolean Whether the choice is a group
|
||||
*/
|
||||
static public function isChoiceGroup($choice)
|
||||
{
|
||||
return is_array($choice) || $choice instanceof \Traversable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the given choice is selected.
|
||||
*
|
||||
* @param mixed $choice The choice
|
||||
*
|
||||
* @return Boolean Whether the choice is selected
|
||||
*/
|
||||
static public function isChoiceSelected($choice, $value)
|
||||
{
|
||||
$choice = FormUtil::toArrayKey($choice);
|
||||
|
||||
// The value should already have been converted by value transformers,
|
||||
// otherwise we had to do the conversion on every call of this method
|
||||
if (is_array($value)) {
|
||||
return false !== array_search($choice, $value, true);
|
||||
}
|
||||
|
||||
return $choice === $value;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user