Skip to content

Commit

Permalink
moved some FormView methods to FormUtil where they really belongs
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Jun 8, 2011
1 parent 1aabc5d commit 566511e
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 51 deletions.
25 changes: 19 additions & 6 deletions src/Symfony/Bridge/Twig/Extension/FormExtension.php
Expand Up @@ -14,6 +14,7 @@
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.
Expand Down Expand Up @@ -74,15 +75,27 @@ public function getTokenParsers()
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
*
Expand Down
Expand Up @@ -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 %}
Expand Down
Expand Up @@ -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>
Expand Down
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Templating\EngineInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\Exception\FormException;
use Symfony\Component\Form\Util\FormUtil;

/**
*
Expand All @@ -38,6 +39,16 @@ public function __construct(EngineInterface $engine)
$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.
*
Expand Down
34 changes: 0 additions & 34 deletions src/Symfony/Component/Form/FormView.php
Expand Up @@ -11,8 +11,6 @@

namespace Symfony\Component\Form;

use Symfony\Component\Form\Util\FormUtil;

class FormView implements \ArrayAccess, \IteratorAggregate, \Countable
{
private $vars = array(
Expand Down Expand Up @@ -275,38 +273,6 @@ public function getIterator()
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.
*
Expand Down
36 changes: 34 additions & 2 deletions src/Symfony/Component/Form/Util/FormUtil.php
Expand Up @@ -13,7 +13,7 @@

abstract class FormUtil
{
public static function toArrayKey($value)
static public function toArrayKey($value)
{
if ((string) (int) $value === (string) $value) {
return (int) $value;
Expand All @@ -26,8 +26,40 @@ public static function toArrayKey($value)
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;
}
}

0 comments on commit 566511e

Please sign in to comment.