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
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 asnew $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 toself::PROPERTY_CONSTRAINT. You should override it only if you want your constraint to work on the whole classself::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 theExtensioninterface. If you decide to add some options under themykey of theapp/config/config.yml, they will be passed as first argument of the->load()method.The
nameof the tag will be used by the FrameworkBundle to find all the constraint validator services and register them into theConstraintValidatorFactory(the part that is responsible of returning the right validator for a given constraint). Thealiasmust be the same string as the one returned by the->validatedBy()method.And you’re right, the namespace registration stuff is not necessary.