8000 merged branch fabpot/validator-xml-loader (PR #8831) · enumag/symfony@c3301dd · GitHub
[go: up one dir, main page]

Skip to content

Commit c3301dd

Browse files
committed
merged branch fabpot/validator-xml-loader (PR symfony#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 | symfony#5603 | License | MIT | Doc PR | n/a ping @bschussek Commits ------- 33b0a17 [Validator] fixed Boolean handling in XML constraint mappings (closes symfony#5603)
2 parents 2165d5d + 33b0a17 commit c3301dd

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

src/Symfony/Component/Validator/Mapping/Loader/XmlFileLoader.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,10 @@ protected function parseOptions(\SimpleXMLElement $nodes)
166166
$value = array();
167167
}
168168
} else {
169-
$value = trim($node);
169+
$value = XmlUtils::phpize($node);
170+
if (is_string($value)) {
171+
$value = trim($value);
172+
}
170173
}
171174

172175
$options[(string) $node['name']] = $value;

src/Symfony/Component/Validator/Tests/Mapping/Loader/XmlFileLoaderTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Validator\Constraints\NotNull;
1717
use Symfony\Component\Validator\Constraints\Range;
1818
use Symfony\Component\Validator\Constraints\Choice;
19+
use Symfony\Component\Validator\Constraints\Regex;
1920
use Symfony\Component\Validator\Mapping\ClassMetadata;
2021
use Symfony\Component\Validator\Mapping\Loader\XmlFileLoader;
2122
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
@@ -68,6 +69,22 @@ public function testLoadClassMetadata()
6869
$this->assertEquals($expected, $metadata);
6970
}
7071

72+
public function testLoadClassMetadataWithNonStrings()
73+
{
74+
$loader = new XmlFileLoader(__DIR__.'/constraint-mapping-non-strings.xml');
75+
$metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
76+
77+
$loader->loadClassMetadata($metadata);
78+
79+
$expected = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
80+
$expected->addPropertyConstraint('firstName', new Regex(array('pattern' => '/^1/', 'match' => false)));
81+
82+
$properties = $metadata->getPropertyMetadata('firstName');
83+
$constraints = $properties[0]->getConstraints();
84+
85+
$this->assertFalse($constraints[0]->match);
86+
}
87+
7188
public function testLoadGroupSequenceProvider()
7289
{
7390
$loader = new XmlFileLoader(__DIR__.'/constraint-mapping.xml');
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" ?>
2+
3+
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
6+
7+
<namespace prefix="custom">Symfony\Component\Validator\Tests\Fixtures\</namespace>
8+
9+
<class name="Symfony\Component\Validator\Tests\Fixtures\Entity">
10+
<property name="firstName">
11+
<!-- Constraint with a Boolean -->
12+
<constraint name="Regex">
13+
<option name="pattern">/^1/</option>
14+
<option name="match">false</option>
15+
</constraint>
16+
</property>
17+
</class>
18+
19+
</constraint-mapping>

src/Symfony/Component/Validator/Tests/Mapping/Loader/constraint-mapping.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
<!-- Constraint with options -->
7979
<constraint name="Choice">
8080
<!-- Option with single value -->
81-
<option name="message">Must be one of %choices%</option>
81+
<option name="message"> Must be one of %choices% </option>
8282
<!-- Option with multiple values -->
8383
<option name="choices">
8484
<value>A</value>

0 commit comments

Comments
 (0)
0