[Validator] fixed Boolean handling in XML constraint mappings (closes #5603)

This commit is contained in:
Fabien Potencier 2013-08-22 21:40:01 +02:00
parent 2165d5dffb
commit 33b0a177b5
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>