merged branch kevindew/form_view_errors (PR #4224)

Commits
-------

e92212a [Form] Added valid attribute to a FormView

Discussion
----------

[Form] Added "valid" to view parameters

Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets:
Todo:
License of the code: MIT
Documentation PR:

This PR adds a parameter of contains_errors to a FormView when buildView is called.

The use case for this addition is when you want to show that a form or sub forms has errors (e.g. when rendering a long form a header message of "This form contains errors" or adding a class to a whole sub form to show an erroneous state) is currently very difficult/near impossible and may need the original form object being accessible in the view layer.

Whats a bit grey here is the best phrasing to use for this. Options I weighed up were is_valid (seemed a bit semantically incorrect in a template, since it would return true pre bind) and has_errors_deep (which i wasn't sure if it fitted naming conventions).

---------------------------------------------------------------------------

by travisbot at 2012-05-07T20:25:55Z

This pull request [fails](http://travis-ci.org/symfony/symfony/builds/1269345) (merged fe1f5aee into 919604ab).

---------------------------------------------------------------------------

by henrikbjorn at 2012-05-19T18:36:33Z

couldnt you just use `{% if errors %}` in your views?

---------------------------------------------------------------------------

by stof at 2012-05-19T19:17:20Z

@henrikbjorn ``errors`` contains only the errors attached to the form itself. It will not tell you when there is some errors in a child form.

---------------------------------------------------------------------------

by kevindew at 2012-05-22T10:43:45Z

Sure, done

---------------------------------------------------------------------------

by travisbot at 2012-05-22T10:45:12Z

This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1399006) (merged 9f6658d4 into 517ae43f).

---------------------------------------------------------------------------

by kevindew at 2012-05-22T10:57:54Z

Shoot, sorry about that.

---------------------------------------------------------------------------

by travisbot at 2012-05-22T11:00:00Z

This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1399096) (merged 88920591 into 517ae43f).

---------------------------------------------------------------------------

by bschussek at 2012-05-22T11:17:38Z

Can you squash the commits and prefix the message with "[Form]" please?

---------------------------------------------------------------------------

by travisbot at 2012-05-22T11:49:52Z

This pull request [fails](http://travis-ci.org/symfony/symfony/builds/1399390) (merged ca771822 into 517ae43f).

---------------------------------------------------------------------------

by kevindew at 2012-05-22T11:53:18Z

Sure, done (I've sorted the travis fail btw)

---------------------------------------------------------------------------

by travisbot at 2012-05-22T11:55:10Z

This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1399404) (merged e92212ac into 517ae43f).

---------------------------------------------------------------------------

by bschussek at 2012-05-22T12:25:45Z

@fabpot 👍
This commit is contained in:
Fabien Potencier 2012-05-22 15:08:07 +02:00
commit 3b58564ed0
2 changed files with 18 additions and 0 deletions

View File

@ -110,6 +110,7 @@ class FormType extends AbstractType
->set('full_name', $fullName)
->set('read_only', $readOnly)
->set('errors', $form->getErrors())
->set('valid', $form->isBound() ? $form->isValid() : true)
->set('value', $form->getClientData())
->set('disabled', $form->isDisabled())
->set('required', $form->isRequired())

View File

@ -16,6 +16,7 @@ use Symfony\Component\Form\Form;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\Tests\Fixtures\Author;
use Symfony\Component\Form\Tests\Fixtures\FixedDataTransformer;
use Symfony\Component\Form\FormError;
class FormTest_AuthorWithoutRefSetter
{
@ -604,4 +605,20 @@ class FormTypeTest extends TypeTestCase
$this->assertEquals(new PropertyPath('foo'), $form->getPropertyPath());
$this->assertFalse($form->getConfig()->getMapped());
}
public function testViewValidUnbound()
{
$form = $this->factory->create('form');
$view = $form->createView();
$this->assertTrue($view->get('valid'));
}
public function testViewNotValidBound()
{
$form = $this->factory->create('form');
$form->bind(array());
$form->addError(new FormError('An error'));
$view = $form->createView();
$this->assertFalse($view->get('valid'));
}
}