Skip to content

Commit

Permalink
[Form] Fixed: Default value of 'error_bubbling' is now determined by …
Browse files Browse the repository at this point in the history
…the 'single_control' option
  • Loading branch information
webmozart committed Apr 27, 2012
1 parent d3bb4d0 commit 246c885
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 61 deletions.
8 changes: 7 additions & 1 deletion src/Symfony/Component/Form/Extension/Core/Type/FormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,12 @@ public function getDefaultOptions()
};
};

// For any form that is not represented by a single HTML control,
// errors should bubble up by default
$errorBubbling = function (Options $options) {
return !$options['single_control'];
};

return array(
'data' => null,
'data_class' => $dataClass,
Expand All @@ -205,7 +211,7 @@ public function getDefaultOptions()
'pattern' => null,
'property_path' => null,
'by_reference' => true,
'error_bubbling' => false,
'error_bubbling' => $errorBubbling,
'error_mapping' => array(),
'label' => null,
'attr' => array(),
Expand Down
7 changes: 2 additions & 5 deletions src/Symfony/Component/Form/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,7 @@ public function __construct($name, EventDispatcherInterface $dispatcher,
$this->validators = $validators;
$this->required = (Boolean) $required;
$this->disabled = (Boolean) $disabled;
// NULL is the default meaning:
// bubble up if the form has children (complex forms)
// don't bubble up if the form has no children (primitive fields)
$this->errorBubbling = null === $errorBubbling ? null : (Boolean) $errorBubbling;
$this->errorBubbling = (Boolean) $errorBubbling;
$this->emptyData = $emptyData;
$this->attributes = $attributes;

Expand Down Expand Up @@ -665,7 +662,7 @@ public function addError(FormError $error)
*/
public function getErrorBubbling()
{
return null === $this->errorBubbling ? $this->hasChildren() : $this->errorBubbling;
return $this->errorBubbling;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -568,4 +568,32 @@ public function testCreateViewDoNoMarkItAsRendered()

$this->assertFalse($view->isRendered());
}

public function testErrorBubblingIfNoSingleControl()
{
$form = $this->factory->create('form', null, array(
'single_control' => false,
));

$this->assertTrue($form->getErrorBubbling());
}

public function testNoErrorBubblingIfSingleControl()
{
$form = $this->factory->create('form', null, array(
'single_control' => true,
));

$this->assertFalse($form->getErrorBubbling());
}

public function testOverrideErrorBubbling()
{
$form = $this->factory->create('form', null, array(
'single_control' => true,
'error_bubbling' => true,
));

$this->assertTrue($form->getErrorBubbling());
}
}
55 changes: 0 additions & 55 deletions src/Symfony/Component/Form/Tests/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,61 +133,6 @@ public function testDataIsInitializedEmpty()
$this->assertSame('bar', $form->getClientData());
}

public function testErrorsBubbleUpIfEnabled()
{
$error = new FormError('Error!');
$parent = $this->form;
$form = $this->getBuilder()->setErrorBubbling(true)->getForm();

$form->setParent($parent);
$form->addError($error);

$this->assertEquals(array(), $form->getErrors());
$this->assertEquals(array($error), $parent->getErrors());
}

public function testErrorsDontBubbleUpIfDisabled()
{
$error = new FormError('Error!');
$parent = $this->form;
$form = $this->getBuilder()->setErrorBubbling(false)->getForm();

$form->setParent($parent);
$form->addError($error);

$this->assertEquals(array($error), $form->getErrors());
$this->assertEquals(array(), $parent->getErrors());
}

public function testErrorsBubbleUpIfNullAndChildren()
{
$error = new FormError('Error!');
$parent = $this->form;
$form = $this->getBuilder()
->setErrorBubbling(null)
->add($this->getBuilder('child'))
->getForm();

$form->setParent($parent);
$form->addError($error);

$this->assertEquals(array(), $form->getErrors());
$this->assertEquals(array($error), $parent->getErrors());
}

public function testErrorsDontBubbleUpIfNullAndNoChildren()
{
$error = new FormError('Error!');
$parent = $this->form;
$form = $this->getBuilder()->setErrorBubbling(null)->getForm();

$form->setParent($parent);
$form->addError($error);

$this->assertEquals(array($error), $form->getErrors());
$this->assertEquals(array(), $parent->getErrors());
}

public function testValidIfAllChildrenAreValid()
{
$this->form->add($this->getValidForm('firstName'));
Expand Down

0 comments on commit 246c885

Please sign in to comment.