Skip to content

Commit

Permalink
[Form] Implement a fluid interface FormView
Browse files Browse the repository at this point in the history
  • Loading branch information
vicb committed May 15, 2011
1 parent fd3a853 commit 39efc49
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 26 deletions.
Expand Up @@ -27,8 +27,10 @@ public function buildForm(FormBuilder $builder, array $options)

public function buildView(FormView $view, FormInterface $form)
{
$view->set('value', $form->getAttribute('value'));
$view->set('checked', (Boolean) $form->getData());
$view
->set('value', $form->getAttribute('value'))
->set('checked', (Boolean) $form->getData())
;
}

public function getDefaultOptions(array $options)
Expand Down
14 changes: 8 additions & 6 deletions src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php
Expand Up @@ -89,12 +89,14 @@ public function buildView(FormView $view, FormInterface $form)
$choices = $form->getAttribute('choice_list')->getChoices();
$preferred = array_flip($form->getAttribute('preferred_choices'));

$view->set('multiple', $form->getAttribute('multiple'));
$view->set('expanded', $form->getAttribute('expanded'));
$view->set('preferred_choices', array_intersect_key($choices, $preferred));
$view->set('choices', array_diff_key($choices, $preferred));
$view->set('separator', '-------------------');
$view->set('empty_value', '');
$view
->set('multiple', $form->getAttribute('multiple'))
->set('expanded', $form->getAttribute('expanded'))
->set('preferred_choices', array_intersect_key($choices, $preferred))
->set('choices', array_diff_key($choices, $preferred))
->set('separator', '-------------------')
->set('empty_value', '')
;

if ($view->get('multiple') && !$view->get('expanded')) {
// Add "[]" to the name in case a select tag with multiple options is
Expand Down
Expand Up @@ -38,8 +38,10 @@ public function buildForm(FormBuilder $builder, array $options)

public function buildView(FormView $view, FormInterface $form)
{
$view->set('allow_add', $form->getAttribute('allow_add'));
$view->set('allow_delete', $form->getAttribute('allow_delete'));
$view
->set('allow_add', $form->getAttribute('allow_add'))
->set('allow_delete', $form->getAttribute('allow_delete'))
;
}

public function getDefaultOptions(array $options)
Expand Down
26 changes: 14 additions & 12 deletions src/Symfony/Component/Form/Extension/Core/Type/FieldType.php
Expand Up @@ -64,18 +64,20 @@ public function buildView(FormView $view, FormInterface $form)
$name = $form->getName();
}

$view->set('form', $view);
$view->set('id', $id);
$view->set('name', $name);
$view->set('errors', $form->getErrors());
$view->set('value', $form->getClientData());
$view->set('read_only', $form->isReadOnly());
$view->set('required', $form->isRequired());
$view->set('max_length', $form->getAttribute('max_length'));
$view->set('size', null);
$view->set('label', $form->getAttribute('label'));
$view->set('multipart', false);
$view->set('attr', array());
$view
->set('form', $view)
->set('id', $id)
->set('name', $name)
->set('errors', $form->getErrors())
->set('value', $form->getClientData())
->set('read_only', $form->isReadOnly())
->set('required', $form->isRequired())
->set('max_length', $form->getAttribute('max_length'))
->set('size', null)
->set('label', $form->getAttribute('label'))
->set('multipart', false)
->set('attr', array())
;

$types = array();
foreach (array_reverse((array) $form->getTypes()) as $type) {
Expand Down
6 changes: 4 additions & 2 deletions src/Symfony/Component/Form/Extension/Core/Type/RadioType.php
Expand Up @@ -27,8 +27,10 @@ public function buildForm(FormBuilder $builder, array $options)

public function buildView(FormView $view, FormInterface $form)
{
$view->set('value', $form->getAttribute('value'));
$view->set('checked', (Boolean) $form->getData());
$view
->set('value', $form->getAttribute('value'))
->set('checked', (Boolean) $form->getData())
;

if ($view->hasParent()) {
$view->set('name', $view->getParent()->get('name'));
Expand Down
6 changes: 4 additions & 2 deletions src/Symfony/Component/Form/Extension/Core/Type/TimeType.php
Expand Up @@ -73,8 +73,10 @@ public function buildForm(FormBuilder $builder, array $options)

public function buildView(FormView $view, FormInterface $form)
{
$view->set('widget', $form->getAttribute('widget'));
$view->set('with_seconds', $form->getAttribute('with_seconds'));
$view
->set('widget', $form->getAttribute('widget'))
->set('with_seconds', $form->getAttribute('with_seconds'))
;
}

public function getDefaultOptions(array $options)
Expand Down
20 changes: 20 additions & 0 deletions src/Symfony/Component/Form/FormView.php
Expand Up @@ -38,10 +38,14 @@ class FormView implements \ArrayAccess, \IteratorAggregate, \Countable
/**
* @param string $name
* @param mixed $value
*
* @return FormView The current view
*/
public function set($name, $value)
{
$this->vars[$name] = $value;

return $this;
}

/**
Expand Down Expand Up @@ -91,10 +95,14 @@ public function getVars()
*
* @param string $name The name of the attribute
* @param string $value The value
*
* @return FormView The current view
*/
public function setAttribute($name, $value)
{
$this->vars['attr'][$name] = $value;

return $this;
}

/**
Expand All @@ -109,20 +117,28 @@ public function isRendered()

/**
* Marks the attached form as rendered
*
* @return FormView The current view
*/
public function setRendered()
{
$this->rendered = true;

return $this;
}

/**
* Sets the parent view.
*
* @param FormView $parent The parent view
*
* @return FormView The current view
*/
public function setParent(FormView $parent = null)
{
$this->parent = $parent;

return $this;
}

/**
Expand All @@ -149,10 +165,14 @@ public function hasParent()
* Sets the children view.
*
* @param array $children The children as instances of FormView
*
* @return FormView The current view
*/
public function setChildren(array $children)
{
$this->children = $children;

return $this;
}

/**
Expand Down

0 comments on commit 39efc49

Please sign in to comment.