-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[FrameworkBundle] Added friendly exception when constraint validator class does not exist #19601
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
234116b
to
58a2f49
Compare
*/ | ||
public function getInstance(Constraint $constraint) | ||
{ | ||
$name = $constraint->validatedBy(); | ||
|
||
if (!isset($this->validators[$name])) { | ||
if (!class_exists($name)) { | ||
throw new InvalidArgumentException(sprintf('Constraint validator class "%s" does not exist.', $name)); |
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.
It's the correct exception class in this case? you could improve the text message? see the PR description.
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.
Maybe a simple ValidatorException
as used in the ValidatorBuilder
?
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.
Thanks, agree with you.
Now, what do you think about the message ? Should be a little bit friendly ?
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 think it's friendly enough, but perhaps you can mention the validatedBy
method.
d923926
to
f070965
Compare
Updated! both exception class and text message have been improved. @ogizanagi thanks one more time. |
d444bed
to
80dda7c
Compare
* @throws UnexpectedTypeException When the validator is not an instance of ConstraintValidatorInterface | ||
*/ | ||
public function getInstance(Constraint $constraint) | ||
{ | ||
$name = $constraint->validatedBy(); | ||
|
||
if (!isset($this->validators[$name])) { | ||
if (!class_exists($name)) { | ||
throw new ValidatorException(sprintf( |
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.
on one line please
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.
fixed 👍
80dda7c
to
0142a83
Compare
* @throws UnexpectedTypeException When the validator is not an instance of ConstraintValidatorInterface | ||
*/ | ||
public function getInstance(Constraint $constraint) | ||
{ | ||
$name = $constraint->validatedBy(); | ||
|
||
if (!isset($this->validators[$name])) { | ||
if (!class_exists($name)) { | ||
throw new ValidatorException(sprintf('Constraint validator "%s" does not exist or it is not enabled. Check the "validatedBy" method in your constraint class "%s"', $name, get_class($constraint))); |
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.
Missing dot at the end of the exception message.
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.
Fixed!
As it already throws an exception, I think this should be merged in 2.7. ping @symfony/deciders |
…it is not enabled
0142a83
to
18cd50c
Compare
@fabpot I should create a separate PR for 2.7 branch? |
👍 as a bug fix |
1 similar comment
👍 as a bug fix |
Thank you @yceruto. |
… validator class does not exist (yceruto) This PR was submitted for the master branch but it was merged into the 2.7 branch instead (closes #19601). Discussion ---------- [FrameworkBundle] Added friendly exception when constraint validator class does not exist | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Currently when mistakenly we type a [Custom Constraint Validator class](http://symfony.com/doc/current/validation/custom_constraint.html#creating-the-validator-itself) or the "alias name" from [validator service](http://symfony.com/doc/current/validation/custom_constraint.html#constraint-validators-with-dependencies) (which would occurs frequently for newcomers) is shown `ClassNotFoundException`: > Attempted to load class "alias_name" from namespace "Symfony\Component\Validator". Did you forget a "use" statement for another namespace? 500 Internal Server Error - ClassNotFoundException **This PR tries to improve the error message when this happen.** But I'm not sure about the exception class used ([`InvalidArgumentException`](https://github.com/yceruto/symfony/blob/master/src/Symfony/Component/Validator/Exception/InvalidArgumentException.php)) : ? * [`ConstraintDefinitionException`](https://github.com/yceruto/symfony/blob/master/src/Symfony/Component/Validator/Exception/ConstraintDefinitionException.php) would be another option, because the source of the error comes from the custom [`Constraint`](https://github.com/yceruto/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Validator/ConstraintValidatorFactory.php#L68) definition. The text message more convenient from this little mistake: ? * This mistake happen for two reason: FQCN or alias name supplied by constraint not found. * The constraint validator service was declared incorrectly (missing alias) * Perhaps some hint how the developer should resolve the mistake. Maybe some documentation core member would help me ? Commits ------- b66ea5e added friendly exception when constraint validator does not exist or it is not enabled
@yceruto FYI, we are able to merge PR on a different branch without you having to create a new PR :) |
Currently when mistakenly we type a Custom Constraint Validator class or the "alias name" from validator service (which would occurs frequently for newcomers) is shown
ClassNotFoundException
:This PR tries to improve the error message when this happen.
But I'm not sure about the exception class used (
InvalidArgumentException
) : ?ConstraintDefinitionException
would be another option, because the source of the error comes from the customConstraint
definition.The text message more convenient from this little mistake: ?
Maybe some documentation core member would help me ?