Skip to content

Commit

Permalink
merged branch bschussek/issue3903 (PR #4341)
Browse files Browse the repository at this point in the history
Commits
-------

1422506 [Form] Clarified the usage of "constraints" in the UPGRADE file
af41a1a [Form] Fixed typos
ac69394 [Form] Allowed native framework errors to be mapped as well
59d6b55 [Form] Fixed: error mapping aborts if reaching an unsynchronized form
9eda5f5 [Form] Fixed: RepeatedType now maps all errors to the first field
215b687 [Form] Added capability to process "." rules in "error_mapping"
c9c4900 [Form] Fixed: errors are not mapped to unsynchronized forms anymore
c8b61d5 [Form] Renamed FormMapping to MappingRule and moved some logic there to make rules more extendable
d0d1fe6 [Form] Added more information to UPGRADE and CHANGELOG
0c09a0e [Form] Made $name parameters optional in PropertyPathBuilder:replaceBy(Index|Property)
081c643 [Form] Updated UPGRADE and CHANGELOG
bbffd1b [Form] Fixed index checks in PropertyPath classes
ea5ff77 [Form] Fixed issues mentioned in the PR comments
7a4ba52 [EventDispatcher] Added class UnmodifiableEventDispatcher
306324e [Form] Greatly improved the error mapping done in DelegatingValidationListener
8f7e2f6 [Validator] Fixed: @Valid does not recurse the traversal of collections anymore by default
5e87dd8 [Form] Added tests for the case when "property_path" is null or false. Instead of setting "property_path" to false, you should set "mapped" to false instead.
2301b15 [Form] Tightened PropertyPath validation to reject any empty value (such as false)
7ff2a9b Revert "[Form] removed a constraint in PropertyPath as the path can definitely be an empty string for errors attached on the main form (when using a constraint defined with the 'validation_constraint' option)"
860dd1f [Form] Adapted Form to create a deterministic property path by default
03f5058 [Form] Fixed property name in PropertyPathMapperTest
c2a243f [Form] Made PropertyPath deterministic: "[prop]" always refers to indices (array or ArrayAccess), "prop" always refers to properties
2996340 [Form] Extracted FormConfig class to simplify the Form's constructor

Discussion
----------

[Form] Improved the error mapping and made property paths deterministic

Bug fix: yes
Feature addition: no
Backwards compatibility break: **yes**
Symfony2 tests pass: yes
Fixes the following tickets: #1971, #2945, #3272, #3308, #3903, #4329
Probably fixes: #2729
Todo: -

This PR is ready for review.

The algorithm for assigning errors to forms in the form tree was improved a lot. Also the error mapping works better now. There are still a few features to be added (e.g. wildcards "*"), but these can be implemented now pretty easily.

This PR breaks PR in that a form explicitely needs to set the "data_class" option if it wants to map to an object and needs to leave that option empty if it wants to map to an array.

Furthermore, property paths must be deterministic now: `foo` now only maps to `(g|s)etFoo()`, but not the index `["foo"]` (array or ArrayAccess), while `[foo]` only maps to the latter but not the former. See #3903 for more information.

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

by travisbot at 2012-05-19T21:35:24Z

This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1377086) (merged 9e346990 into 2229461).

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

by Tobion at 2012-05-20T01:47:48Z

Good stuff in general :)

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

by bschussek at 2012-05-20T09:19:18Z

Fixed everything mentioned here so far.

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

by travisbot at 2012-05-20T09:22:22Z

This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1379548) (merged 49918bef into 2229461).

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

by Tobion at 2012-05-20T14:29:14Z

many occurences of two spaces after `@param` (should be only one space).

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

by Koc at 2012-05-20T14:40:18Z

Sorry, I'm cannot observe all changes for form component in 2.1, so I have a question:

```php
<?php

protected $isPrivate;

public function isPrivate() {}

public function setPrivate() {}
```

Is it possible validate this property with accessors/mutators from code above in 2.1 now?

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

by bschussek at 2012-05-20T14:41:09Z

The type after `@param` used to be aligned with the type of the `@return` tag. Let's get the PHPDoc-guidelines straight before nitpicking more on these trivialities.

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

by bschussek at 2012-05-20T14:42:34Z

@Koc Please move your question to the user mailing list, let's keep this PR on topic.

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

by bschussek at 2012-05-20T14:45:42Z

Fixed everything mentioned until now.

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

by travisbot at 2012-05-20T14:47:48Z

This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1380903) (merged 03d60a03 into f433f6b).

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

by bschussek at 2012-05-20T15:18:12Z

CHANGELOG/UPGRADE is now updated.

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

by travisbot at 2012-05-20T15:19:39Z

This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1381047) (merged 48cc3eca into f433f6b).

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

by Tobion at 2012-05-20T16:16:51Z

All the deprecated methods and changed constructor arguments should probably be mentioned in the changelog/upgrade.

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

by travisbot at 2012-05-21T07:31:47Z

This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1386621) (merged c0ef69a1 into 1407f11).

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

by travisbot at 2012-05-21T08:01:46Z

This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1386826) (merged 4f3fc1fe into 1407f11).

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

by travisbot at 2012-05-21T09:22:30Z

This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1387263) (merged e3675050 into 1407f11).

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

by bschussek at 2012-05-21T09:43:08Z

This PR now fixes #1971.

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

by travisbot at 2012-05-21T09:45:51Z

This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1387370) (merged de33f9ef into 1407f11).

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

by travisbot at 2012-05-21T11:06:53Z

This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1387838) (merged da3b562e into 1407f11).

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

by bschussek at 2012-05-21T11:07:45Z

This PR now fixes #2945.

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

by bschussek at 2012-05-21T15:33:33Z

Native errors (such as "invalid", "extra_fields" etc.) are now respected by the "error_mapping" option as well. The option "validation_constraint" was deprecated, "constraints" is its replacement and a lot handier, because it allows you to work easily with arrays.

```php
<?php
$builder
    ->add('name', 'text', array(
        'constraints' => new NotBlank(),
    ))
    ->add('phoneNumber', 'text', array(
        'constraints' => array(
            new NotBlank(),
            new MinLength(7),
            new Type('numeric')
        )
    ));
```

Ready for review again.

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

by travisbot at 2012-05-21T15:33:45Z

This pull request [fails](http://travis-ci.org/symfony/symfony/builds/1390239) (merged e162f56d into ea33d4d).

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

by travisbot at 2012-05-21T15:40:02Z

This pull request [fails](http://travis-ci.org/symfony/symfony/builds/1390367) (merged e8729a7f into ea33d4d).

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

by travisbot at 2012-05-21T16:06:03Z

This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1390663) (merged ef39aba4 into ea33d4d).

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

by travisbot at 2012-05-22T08:54:36Z

This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1398153) (merged af41a1a into e4e3ce6).

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

by travisbot at 2012-05-22T09:26:12Z

This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1398415) (merged 1422506 into e4e3ce6).
  • Loading branch information
fabpot committed May 22, 2012
2 parents 58f4556 + 2ec9bfe commit 06301ef
Show file tree
Hide file tree
Showing 84 changed files with 7,122 additions and 2,526 deletions.
134 changes: 134 additions & 0 deletions UPGRADE-2.1.md
Expand Up @@ -458,6 +458,118 @@
}
}

* Setting the option "property_path" to `false` was deprecated and will be unsupported
as of Symfony 2.3.

You should use the new option "mapped" instead in order to set that you don't want
a field to be mapped to its parent's data.

Before:

```
$builder->add('termsAccepted', 'checkbox', array(
'property_path' => false,
));
```

After:

```
$builder->add('termsAccepted', 'checkbox', array(
'mapped' => false,
));
```

* The "data_class" option now *must* be set if a form maps to an object. If
you leave it empty, the form will expect an array or a scalar value and
fail with a corresponding exception.

Likewise, if a form maps to an array, the option *must* be left empty now.

* The mapping of property paths to arrays has changed.

Previously, a property path "street" mapped to both a field `$street` of
a class (or its accessors `getStreet()` and `setStreet()`) and an index
`['street']` of an array or an object implementing `\ArrayAccess`.

Now, the property path "street" only maps to a class field (or accessors),
while the property path "[street]" only maps to indices.

If you defined property paths manually in the "property_path" option, you
should revise them and adjust them if necessary.

Before:

```
$builder->add('name', 'text', array(
'property_path' => 'address.street',
));
```

After (if the address object is an array):

```
$builder->add('name', 'text', array(
'property_path' => 'address[street]',
));
```

If address is an object in this case, the code given in "Before"
works without changes.

* The following methods in `Form` are deprecated and will be removed in
Symfony 2.3:

* `getTypes`
* `getErrorBubbling`
* `getNormTransformers`
* `getClientTransformers`

You can access these methods on the `FormConfigInterface` object instead.

Before:

```
$form->getErrorBubbling()
```

After:

```
$form->getConfig()->getErrorBubbling();
```

* The option "validation_constraint" is deprecated and will be removed
in Symfony 2.3. You should use the option "constraints" instead,
where you can pass one or more constraints for a form.

Before:

```
$builder->add('name', 'text', array(
'validation_constraint' => new NotBlank(),
));
```

After:

```
$builder->add('name', 'text', array(
'constraints' => new NotBlank(),
));
```

Unlike previously, you can also pass a list of constraints now:

```
$builder->add('name', 'text', array(
'constraints' => array(
new NotBlank(),
new MinLength(3),
),
));
```

### Validator

* The methods `setMessage()`, `getMessageTemplate()` and
Expand Down Expand Up @@ -571,6 +683,28 @@
* Core translation messages are changed. Dot is added at the end of each message.
Overwritten core translations should be fixed if any. More info here.

* Collections (arrays or instances of `\Traversable`) in properties
annotated with `Valid` are not traversed recursively by default anymore.

This means that if a collection contains an entry which is again a
collection, the inner collection won't be traversed anymore as it
happened before. You can set the BC behavior by setting the new property
`deep` of `Valid` to `true`.

Before:

```
/** @Assert\Valid */
private $recursiveCollection;
```

After:

```
/** @Assert\Valid(deep = true) */
private $recursiveCollection;
```

### Session

* Flash messages now return an array based on their type. The old method is
Expand Down
5 changes: 4 additions & 1 deletion src/Symfony/Bundle/FrameworkBundle/Resources/config/form.xml
Expand Up @@ -133,9 +133,12 @@
</service>

<!-- FormTypeValidatorExtension -->
<service id="form.type_extension.field" class="Symfony\Component\Form\Extension\Validator\Type\FormTypeValidatorExtension">
<service id="form.type_extension.form.validator" class="Symfony\Component\Form\Extension\Validator\Type\FormTypeValidatorExtension">
<tag name="form.type_extension" alias="form" />
<argument type="service" id="validator" />
</service>
<service id="form.type_extension.repeated.validator" class="Symfony\Component\Form\Extension\Validator\Type\RepeatedTypeValidatorExtension">
<tag name="form.type_extension" alias="repeated" />
</service>
</services>
</container>
1 change: 1 addition & 0 deletions src/Symfony/Component/EventDispatcher/CHANGELOG.md
Expand Up @@ -13,3 +13,4 @@ CHANGELOG
* added GenericEvent event class
* added the possibility for subscribers to subscribe several times for the
same event
* added UnmodifiableEventDispatcher
@@ -0,0 +1,106 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\EventDispatcher\Tests;

use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\UnmodifiableEventDispatcher;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class UnmodifiableEventDispatcherTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $innerDispatcher;

/**
* @var UnmodifiableEventDispatcher
*/
private $dispatcher;

protected function setUp()
{
$this->innerDispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
$this->dispatcher = new UnmodifiableEventDispatcher($this->innerDispatcher);
}

public function testDispatchDelegates()
{
$event = new Event();

$this->innerDispatcher->expects($this->once())
->method('dispatch')
->with('event', $event)
->will($this->returnValue('result'));

$this->assertSame('result', $this->dispatcher->dispatch('event', $event));
}

public function testGetListenersDelegates()
{
$this->innerDispatcher->expects($this->once())
->method('getListeners')
->with('event')
->will($this->returnValue('result'));

$this->assertSame('result', $this->dispatcher->getListeners('event'));
}

public function testHasListenersDelegates()
{
$this->innerDispatcher->expects($this->once())
->method('hasListeners')
->with('event')
->will($this->returnValue('result'));

$this->assertSame('result', $this->dispatcher->hasListeners('event'));
}

/**
* @expectedException \BadMethodCallException
*/
public function testAddListenerDisallowed()
{
$this->dispatcher->addListener('event', function () { return 'foo'; });
}

/**
* @expectedException \BadMethodCallException
*/
public function testAddSubscriberDisallowed()
{
$subscriber = $this->getMock('Symfony\Component\EventDispatcher\EventSubscriberInterface');

$this->dispatcher->addSubscriber($subscriber);
}

/**
* @expectedException \BadMethodCallException
*/
public function testRemoveListenerDisallowed()
{
$this->dispatcher->removeListener('event', function () { return 'foo'; });
}

/**
* @expectedException \BadMethodCallException
*/
public function testRemoveSubscriberDisallowed()
{
$subscriber = $this->getMock('Symfony\Component\EventDispatcher\EventSubscriberInterface');

$this->dispatcher->removeSubscriber($subscriber);
}
}
@@ -0,0 +1,92 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\EventDispatcher;

/**
* A read-only proxy for an event dispatcher.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class UnmodifiableEventDispatcher implements EventDispatcherInterface
{
/**
* The proxied dispatcher.
* @var EventDispatcherInterface
*/
private $dispatcher;

/**
* Creates an unmodifiable proxy for an event dispatcher.
*
* @param EventDispatcherInterface $dispatcher The proxied event dispatcher.
*/
public function __construct(EventDispatcherInterface $dispatcher)
{
$this->dispatcher = $dispatcher;
}

/**
* {@inheritdoc}
*/
public function dispatch($eventName, Event $event = null)
{
return $this->dispatcher->dispatch($eventName, $event);
}

/**
* {@inheritdoc}
*/
public function addListener($eventName, $listener, $priority = 0)
{
throw new \BadMethodCallException('Unmodifiable event dispatchers must not be modified.');
}

/**
* {@inheritdoc}
*/
public function addSubscriber(EventSubscriberInterface $subscriber)
{
throw new \BadMethodCallException('Unmodifiable event dispatchers must not be modified.');
}

/**
* {@inheritdoc}
*/
public function removeListener($eventName, $listener)
{
throw new \BadMethodCallException('Unmodifiable event dispatchers must not be modified.');
}

/**
* {@inheritdoc}
*/
public function removeSubscriber(EventSubscriberInterface $subscriber)
{
throw new \BadMethodCallException('Unmodifiable event dispatchers must not be modified.');
}

/**
* {@inheritdoc}
*/
public function getListeners($eventName = null)
{
return $this->dispatcher->getListeners($eventName);
}

/**
* {@inheritdoc}
*/
public function hasListeners($eventName = null)
{
return $this->dispatcher->hasListeners($eventName);
}
}
15 changes: 15 additions & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Expand Up @@ -70,3 +70,18 @@ CHANGELOG
* deprecated method `guessMinLength` in favor of `guessPattern`
* labels don't display field attributes anymore. Label attributes can be
passed in the "label_attr" option/variable
* added option "mapped" which should be used instead of setting "property_path" to false
* "data_class" now *must* be set if a form maps to an object and should be left empty otherwise
* improved error mapping on forms
* dot (".") rules are now allowed to map errors assigned to a form to
one of its children
* errors are not mapped to unsynchronized forms anymore
* changed Form constructor to accept a single `FormConfigInterface` object
* changed argument order in the FormBuilder constructor
* deprecated Form methods
* `getTypes`
* `getErrorBubbling`
* `getNormTransformers`
* `getClientTransformers`
* deprecated the option "validation_constraint" in favor of the new
option "constraints"

0 comments on commit 06301ef

Please sign in to comment.