-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Update custom_constraint.rst #6610
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
You have to specify an alias as of ``` //Symfony\Bundle\FrameworkBundle\Validator\ConstraintValidatorFactory.php [...] $name = $constraint->validatedBy(); if (!isset($this->validators[$name])) { $this->validators[$name] = new $name(); //LOOK HERE; IF I DON'T SPECIFY THIS ALIAS, I WILL END HERE; NO SERVICE THERE! } elseif (is_string($this->validators[$name])) { $this->validators[$name] = $this->container->get($this->validators[$name]); } [...] ```
+1 |
@marcoalbarelli yes, is true: I forgot to report this. |
@@ -158,7 +158,7 @@ Constraint Validators with Dependencies | |||
If your constraint validator has dependencies, such as a database connection, | |||
it will need to be configured as a service in the Dependency Injection | |||
Container. This service must include the ``validator.constraint_validator`` | |||
tag and may include an ``alias`` attribute: | |||
tag and *MUST* include an ``alias`` attribute: | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd add that alias
must match what is returned by the validateBy
method and that it must not necessarily match the service id
Just to be on the nitpicking side of things: it should be noted that if the <service id="somebundle.file.validator.base_validator" class="SomeBundle\Validator\Constraints\BaseValidator">
<tag name="validator.constraint_validator" alias="SomeBundle\Validator\Constraints\BaseValidator"/>
<!-- rest of the service definition -->
</service> |
@marcoalbarelli yes but I suppose that's not a Symfony standard anymore that way :) |
I didn't find any explicit constraint or standard on what should be used as an alias in "normal" services definitions, but since the alias should match a service id, which in turn has to be a valid yaml key, and since a FQCN is not a valid yaml key, I think services ids are de-facto standardized to what can be used as a valid yml key. In this context though the term Just for reference: the xsd for the |
I can confirm that there is no standard in service id naming and since 3.1 any utf8 string will be considered as valid. |
After checking the source code of the validator component, I'm closing this in favor of #7489. But I'm not 100% sure, so I've opened symfony/symfony#21610 too. Thanks! |
You MUST specify an alias because
So if custom validator is a service that needs to be taken from
Container
, there is no valid alternative toalias
: it should be mandatory (and should be reported onto documentation) as you can see from this factory.