T
T
troublegum2020-11-29 23:15:31
symfony
troublegum, 2020-11-29 23:15:31

symfony. How to make validation dependent on values ​​of sv-in essence?

Good day to all!
Faced such a problem that it is necessary to make the validation of the St-in the Post entity with a certain business logic.

What is:
- validation through annotations
- the Post entity (publication) with the publication attribute and the publication date
- the publication date is required and must be greater than or equal to the current date if the publication attribute is set
- if the publication attribute is cleared, then the publication date field is not validated in any way

Question ! Is it possible to somehow make such a validation without writing your own validator for the "publication date" field?

class Post
{
  /**
     * @Assert\NotNull
     */
  public bool $published;
  
    /**
     * Как написать правила валидации для этого св-ва?
     * @Assert\DateTime
     */
  public \DateTime $publishedAt;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2020-11-29
@troublegum

It is impossible to write validators for all such business requirements, you can trick it, but then the business rule will go away, which is also difficult to test.
So what can I advise you. Place this business logic inside the entity in a special method. For example published(): void. Example from code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question