8000 Validation Constraint Inheritance Broken · Issue #21567 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
Validation Constraint Inheritance Broken #21567
Closed
@scheb

Description

@scheb
Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no
Symfony version >=3.1.9

Symfony validator does no longer inherit all constraints from the parent class. It seems like all the constraints from super-classes are lost when there are additional constraints on the same attribute in the sub-class. I was able to track it down to this commit, if I roll-back the changes it's working again. But couldn't wrap my head around yes, why this is happening.

Test script to reproduce the bug:

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <class name="Foo">
        <property name="attribute">
            <constraint name="NotNull">
                <option name="message">NOT_NULL</option>
            </constraint>
        </property>
    </class>
    <class name="Bar">
        <property name="attribute">
            <constraint name="Type">
                <option name="type">string</option>
                <option name="message">NOSTRING</option>
            </constraint>
        </property>
    </class>
</constraint-mapping>
<?php
include(__DIR__ . '/vendor/autoload.php');

use Symfony\Component\Validator\Validation;

abstract class Foo {
    public $attribute;
}

class Bar extends Foo {
}

$validator = Validation::createValidatorBuilder()->addXmlMapping(__DIR__ . '/validation.xml')->getValidator();

$test1 = new Bar();
$test1->attribute = null; // NotNull violation
var_dump($validator->validate($test1)->count()); // Should be 1, but is 0 in version >= 3.1.9

$test2 = new Bar();
$test2->attribute = 42; // Type violation
var_dump($validator->validate($test2)->count()); // Should be 1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0