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 8552141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:21:26+00:00 2026-06-11T14:21:26+00:00

I need to create a Symfony 2 custom class constraint validator , to validate

  • 0

I need to create a Symfony 2 custom class constraint validator, to validate that a property is not equal to another (i.e. password can’t match username).

My first question is: do I need to implement method getDefaultOption() and what’s is used for?

/**
 * @Annotation
 */
class NotEqualTo extends Constraint
{
    /**
     * @var string
     */
    public $message = "{{ property1 }} should not be equal to {{ property2 }}.";

    /**
     * @var string
     */
    public $property1;

    /**
     * @var string
     */
    public $property2;

    /**
     * {@inheritDoc}
     */
    public function getRequiredOptions() { return ['property1', 'property2']; }

    /**
     * {@inheritDoc}
     */
    public function getTargets() { return self::CLASS_CONSTRAINT; }
}

Second question is, how do I get the actual object (to check for “property1” and “property2”) in my validate() method?

public function validate($value, Constraint $constraint)
{
    if(null === $value || '' === $value) {
        return;
    }

    if (!is_string($constraint->property1)) {
        throw new UnexpectedTypeException($constraint->property1, 'string');
    }

    if (!is_string($constraint->property2)) {
        throw new UnexpectedTypeException($constraint->property2, 'string');
    }

    // Get the actual value of property1 and property2 in the object

    // Check for equality
    if($object->property1 === $object->property2) {
        $this->context->addViolation($constraint->message, [
            '{{ property1 }}' => $constraint->property1,
            '{{ property2 }}' => $constraint->property2,
        ]);
    }
}
  • 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-06-11T14:21:27+00:00Added an answer on June 11, 2026 at 2:21 pm

    do I need to implement method getDefaultOption() and what’s is used for?

    You don’t have to do that, however it’s strongly recommended to do so if your annotation has a single “leading” property. Annotation’s properties are defined as a list of key-value pairs, eg:

    @MyAnnotation(paramA = "valA", paramB = "valB", paramC = 123)
    @MaxValue(value = 199.99)
    

    Using getDefaultOption() you can tell the annotations processor which option is a default one. If you’d define paramA as a default option of @MyAnnotation and value as a default option of @MaxValue you’d be able to write:

    @MyAnnotation("valA", paramB = "valB", paramC = 123)
    @MaxValue(199.99)
    @MaxValue(199.99, message = "The value has to be lower than 199.99")
    

    how do I get the actual object (to check for “property1” and “property2”) in my validate() method?

    You have to create a class-level constraint annotation. Then $value argument in your validate() method will be a whole object rather a single property.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need create clone repository. but I do not know where can I get
I need create custom tabbar with super class UITabBarController. But i don,t know how
I need to create a form class,I'm following the symfony book in http://symfony.com/doc/current/book/forms.html I
I am confused that in which file i need to create symfony functions. E,g
I need create custom dialog and put JPanel into it. Is it possible?
I need create a document word with Java. And I ask, how can I
i need create a variable with parent subclass. Example: Parent Class <?php class parentClass
Need to create a custom DNS name server using C which will check against
I need to create a class with two properties: LogOutput ExceptionOutput These properties (Actions<>)
How can I create a new project that only uses the Symfony2 Console component?

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.