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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:29:11+00:00 2026-05-27T06:29:11+00:00

I am reading a tutorial and there are some parts I dont understand: public

  • 0

I am reading a tutorial and there are some parts I dont understand:

public function validatedBy()
{
    return 'validator.unique';
}

Is this a good practice or should I just use Symfony’s default of get_class($this).'Validator' (from docs)

public function requiredOptions()
{
    return array('entity', 'property');
}

public function targets()
{
    return self::PROPERTY_CONSTRAINT;
}

Are the above methods required? It doesn’t appear in the docs

# MyApp/MyBundle/Resources/config/services.yml
parameters:
    my.validator.unique.class:      MyApp\MyBundle\Validator\UniqueValidator

services:
    my.validator.unique:
       class:      %my.validator.unique.class%
       arguments: [@doctrine.orm.entity_manager]
       tags:
            - { name: validator.constraint_validator, alias: validator.unique }

is that parameter declaration simply so that I can use %my.validator.unique.class%, instead of the fully qualified class name?

About the name & alias. Am I right to say name is the “type” of constraint. alias is the alias used by Constraint::validatedBy()

# Extension class
public function getAlias() {
    return 'my';
}

Is the above required? I dont see my used anywhere?

// get the existing registered namespaces for validator annotations
$namespaces = $container->getParameter('validator.annotations.namespaces');
// add our namespace under the alias myvalidation
$namespaces['myvalidation'] = 'MyApp\\MyBundle\\Validator\\';
// save it
$container->setParameter('validator.annotations.namespaces', $namespaces);

It seems just @Annotation in the Constraint class works? validator.annotations.namespaces gives an error about non-existant param

  • 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-27T06:29:12+00:00Added an answer on May 27, 2026 at 6:29 am

    About the ->validatedBy() method, returning a simple class name will work only if your validator class does not have any dependency because the validator will try to create it using something as simple as new $classname(). The only problem (if it’s really a problem…) with use of the service name is that you couple your constraint to the FrameworkBundle.

    The ->getRequiredOptions() method is empty by default so if you have any required option, it’s a good practice too override this method.

    The ->getTargets() method already defaults to self::PROPERTY_CONSTRAINT. You should override it only if you want your constraint to work on the whole class self::CLASS_CONSTRAINT (you can even return an array if you want your constraint to work on both cases).

    In your service definition, you made the choice of using the %my.validator.unique.class% parameter to store the class name for themy.validator.unique` service.

    About the ->getName() method, it’s part of the Extension interface. If you decide to add some options under the my key of the app/config/config.yml, they will be passed as first argument of the ->load() method.

    The name of the tag will be used by the FrameworkBundle to find all the constraint validator services and register them into the ConstraintValidatorFactory (the part that is responsible of returning the right validator for a given constraint). The aliasmust be the same string as the one returned by the ->validatedBy() method.

    And you’re right, the namespace registration stuff is not necessary.

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

Sidebar

Related Questions

I was reading some assembly tutorial in which there were explained the signed integers
I'm reading some tutorials now about jQuery.. function creating,plugin creation etc. but these tutorials
I'm learning by reading this tutorial: Link Here's the code: <?php require_once 'Zend/Loader.php'; class
I was reading html5 video tutorial which suggested to use some player for html5
I am reading a tutorial that uses the following example (that I'll generalize somewhat):
I am reading the tutorial at http://code.google.com/p/google-web-toolkit-incubator/wiki/LoginSecurityFAQ It states Remember - you must never
I was reading a tutorial on ASP.NET and third party API's and it mentioned
I was reading over the tutorial here: http://www.1keydata.com/sql/sql-running-totals.html and it all made sense until
I've been reading many a tutorial/article on unmanaged DLLs in C++. For the life
I'm just getting started with ASP.NET MVC, mostly by reading ScottGu's tutorial . To

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.