Symfony 4注释约束不适用于继承的属性

时间:2019-11-05 20:39:26

标签: validation inheritance symfony4

我正在尝试将验证规则应用于从另一个实体继承的子实体的属性。

父实体:

/**
 * Cargo
 * @ORM\Entity(repositoryClass="App\Repository\Cargo\CargoRepository")
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="discr", type="string")
 * @ORM\DiscriminatorMap({"cargo" = "Cargo", "maritime_container" = "MaritimeContainer", "pallet" = "Pallet",
 *     "swap_unit" = "SwapUnit", "unit_load" = "UnitLoad", "furniture" = "Furniture"})
 *
 * @UniqueEntity(fields={"trip"}, message="This trip has been associated!")
 */

class Cargo
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var float
     *
     * @Assert\NotNull()
     * @Assert\Positive()
     * @ORM\Column(name="gross_weight", type="float", nullable=false)
     */
    public $grossWeight;
}

子实体:

/**
 * MaritimeContainer
 *
 * @ORM\Entity(repositoryClass="App\Repository\Cargo\MaritimeContainerRepository")
 */
class MaritimeContainer extends Cargo
{

    /**
     * @var string
     *
     * @ORM\Column(name="feet_container", type="string", length=2)
     *
     * @Assert\NotBlank()
     * @Assert\Choice(choices={"20", "40"}, strict=true)
     * @Assert\Length(min=2, max=2)
     */
    public $feetContainer;
}

在这种情况下,Validator没有检测到违规,它应该告诉我gross_weight应该为正值。即使我不使用此setter,它也应告诉gross_weight不能为null,并且不会:

$maritimeContainer = new MaritimeContainer();
$maritimeContainer->setGrossWeight(-5);

任何建议都会得到赞赏。

0 个答案:

没有答案