Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7083557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:11:34+00:00 2026-05-28T07:11:34+00:00

I have an entity ArticlePattern , which has a property pattern (string). I need

  • 0

I have an entity ArticlePattern, which has a property pattern (string). I need to access the database to check if pattern is correct. So I would like to define a method ArticlePattern::isPatternValid() and add a constraint (using Doctrine’s annotation) which would check if isPatternValid is true during validation by Validator object.

From what I have read here and there it is not a good idea, to make an entity depended on service container, which mean I cannot access the doctrine service from inside ArticlePattern::isPatternValid().

So how can I make a custom validation constraint which need an access to the database? How do you deal with such situations which I think is very common seeing so many questions about accessing a service container from an entity class.

EDIT:

Ok, thanks guys, so the answer is a Custom Validation Constraint

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:11:35+00:00Added an answer on May 28, 2026 at 7:11 am

    A validator object can be:

    • A simple object, that has no connection to the framework environment at all.
    • A service (in the context of dependency injection container) which could do absolutley anything as long as it impements Symfony\Component\Validator\ConstraintValidatorInterface

    So what do you have to do?

    1. Define a simple constraint
    2. Override validatedBy() method to return validator “name” (return 'my_validator';)
    3. Define a simple service in DIC:

      <service id="project.validator.my" class="Project\Constraints\MyValidator">
          <!-- service definition here -->
      
          <!-- the service has to be tagged -->
          <tag name="validator.constraint_validator" alias="my_validator" />
      </service>
      

    EDIT

    You’ve asked about multiple properties validation. In such a case you could create a validator that is related to the object rather to the property of the object.

    1. In your constraint class define the target of that constraint (property / class):

      class MyConstraint ... {
          ...
      
          public function targets() {
              return self::CLASS_CONSTRAINT;
          }
      }
      
    2. Annotate validated class instead of property:

      @Assert/MyConstraint(...)
      class MyClass {
          private $firstName;
          private $lastName;
      
          @Assert/Email
          private $email;
      
          ...
      }
      
    3. The validator itself looks pretty much the same as in case of validating a property:

      class MyValidator extends ConstraintValidator {
          public function isValid($value, Constraint $constraint) {
              // $value is an object rather a property
          }
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an entity which has a property that does not get its value
I have entity Question . Entity Question has property Number . I need to
I have entity Ideas, which has child entity collection ChildIdeas. I need to load
I have an entity class that has a property with an underlying db column
I have an Entity result of List<string> GetTagsData that I need to somehow bind
suppose i have @Entity A, which has an eager @ManyToOne to @Entity B, and
I have some entity types that I would like to lazy load. However, they
I have entity A which has an IList of B called Bs and B
I have a entity which has a list of Child1 objects. The mappings seem
I have entity Recipes and it has a HasMany collection comments. In a MVC

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.