Home > OS >  Regular expression doesn't work in class entity symfony
Regular expression doesn't work in class entity symfony

Time:10-19

There are two entities: Product and ProductDetails:

class ProductDetail {

#[ORM\ManyToOne(targetEntity: Product::class, inversedBy: 'productDetail')]
#[ORM\JoinColumn(nullable: false)]
private $productItem;

#[ORM\Column(type: 'date', nullable: true)]
#[Assert\GreaterThan('today')]
#[Assert\Expression(
    expression: 'this.getProductItem()->getStatus() in ['not done'] or value',
    message: 'The planification date cannot be null for this status!',
)]
private $finishedDate;


class Product {

#[ORM\Column(type: 'string', length: 255)]
private $status;

Basically I want to create a constraint for the finishedDate to not allow to be empty when the status is 'not done'. But it doesn't do absolutely nothing and I'm curious if I'm missing anything. Even if I put value there and the date is empty the message is not triggered.

CodePudding user response:

According to this link : https://symfony.com/doc/current/components/expression_language/syntax.html.

Could you try :

this.getProductItem().getStatus()

You can use a custom constraint in your case, it's more flexible : https://symfony.com/doc/current/validation/custom_constraint.html

  • Related