merged branch fabpot/validator-xml-loader (PR #8831)

This PR was merged into the 2.2 branch.

Discussion
----------

[Validator] fixed Boolean handling in XML constraint mappings

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #5603
| License       | MIT
| Doc PR        | n/a

ping @bschussek

Commits
-------

33b0a17 [Validator] fixed Boolean handling in XML constraint mappings (closes #5603)
This commit is contained in:
Fabien Potencier 2013-08-24 08:02:05 +02:00
commit c3301dde87
4 changed files with 41 additions and 2 deletions

View File

@ -166,7 +166,10 @@ class XmlFileLoader extends FileLoader
$value = array();
}
} else {
$value = trim($node);
$value = XmlUtils::phpize($node);
if (is_string($value)) {
$value = trim($value);
}
}
$options[(string) $node['name']] = $value;

View File

@ -16,6 +16,7 @@ use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\Constraints\Range;
use Symfony\Component\Validator\Constraints\Choice;
use Symfony\Component\Validator\Constraints\Regex;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Loader\XmlFileLoader;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
@ -68,6 +69,22 @@ class XmlFileLoaderTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($expected, $metadata);
}
public function testLoadClassMetadataWithNonStrings()
{
$loader = new XmlFileLoader(__DIR__.'/constraint-mapping-non-strings.xml');
$metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
$loader->loadClassMetadata($metadata);
$expected = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
$expected->addPropertyConstraint('firstName', new Regex(array('pattern' => '/^1/', 'match' => false)));
$properties = $metadata->getPropertyMetadata('firstName');
$constraints = $properties[0]->getConstraints();
$this->assertFalse($constraints[0]->match);
}
public function testLoadGroupSequenceProvider()
{
$loader = new XmlFileLoader(__DIR__.'/constraint-mapping.xml');

View File

@ -0,0 +1,19 @@
<?xml version="1.0" ?>
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
<namespace prefix="custom">Symfony\Component\Validator\Tests\Fixtures\</namespace>
<class name="Symfony\Component\Validator\Tests\Fixtures\Entity">
<property name="firstName">
<!-- Constraint with a Boolean -->
<constraint name="Regex">
<option name="pattern">/^1/</option>
<option name="match">false</option>
</constraint>
</property>
</class>
</constraint-mapping>

View File

@ -78,7 +78,7 @@
<!-- Constraint with options -->
<constraint name="Choice">
<!-- Option with single value -->
<option name="message">Must be one of %choices%</option>
<option name="message"> Must be one of %choices% </option>
<!-- Option with multiple values -->
<option name="choices">
<value>A</value>