Symfony: 在自定义约束中添加标准约束

huangapple go评论90阅读模式
英文:

Symfony : adding standard constraint in a custom constraint

问题

I'm trying to add in a custom constraint defined as defined in the official documentation, a built-in constraint (for instance, Url).

So basically, I've added to my validator class, in the validation method:

  1. use Symfony\Component\Validator\Constraints\Url;
  2. use Symfony\Component\Validator\Constraint;
  3. public function validate($value, Constraint $constraint): void
  4. {
  5. [...]
  6. $this->context
  7. ->getValidator()
  8. ->inContext($this->context)
  9. ->validate($value, new Url());
  10. }

as it seems possible in this article.

Unfortunately, it is not working. The violation is not added to the other of the constraint. I have no choice to replace the code by something less concise as :

  1. use Symfony\Component\Validator\Constraints\Url;
  2. use Symfony\Component\Validator\Constraint;
  3. public function validate($value, Constraint $constraint): void
  4. {
  5. [...]
  6. $urlConstraint = new Url();
  7. $violations = $this->context
  8. ->getValidator()
  9. ->validate($value, $urlConstraint);
  10. if (count($violations) !== 0) {
  11. $this->context
  12. ->buildViolation($urlConstraint->message)
  13. ->addViolation();
  14. }
  15. }

Is there any possibility to include the standard constraint to the custom one (without of course adding the constraint directly to the Entity class) ?

英文:

I'm trying to add in a custom constraint defined as defined in the official documentation, a built-in constraint (for instance, Url).

So basically, I've added to my validator class, in the validation method:

  1. use Symfony\Component\Validator\Constraints\Url;
  2. use Symfony\Component\Validator\Constraint;
  3. public function validate($value, Constraint $constraint): void
  4. {
  5. [...]
  6. $this->context
  7. ->getValidator()
  8. ->inContext($this->context)
  9. ->validate($value, new Url());
  10. }

as it seems possible in this article.

Unfortunately, it is not working. The violation is not added to the other of the constraint. I have no choice to replace the code by something less concise as :

  1. use Symfony\Component\Validator\Constraints\Url;
  2. use Symfony\Component\Validator\Constraint;
  3. public function validate($value, Constraint $constraint): void
  4. {
  5. [...]
  6. $urlConstraint = new Url();
  7. $violations = $this->context
  8. ->getValidator()
  9. ->validate($value, $urlConstraint);
  10. if (count($violations) !== 0) {
  11. $this->context
  12. ->buildViolation($urlConstraint->message)
  13. ->addViolation();
  14. }

Is there any possibility to include the standard constraint to the custom one (without of course adding the constraint directly to the Entity class) ?

答案1

得分: 1

在上面的示例中,我没有提到验证是放置在特定的验证组中的。
因此,标准约束未触发。必须将标准约束附加到默认组:

  1. $this->context
  2. ->getValidator()
  3. ->inContext($this->context)
  4. ->validate($value, new Url(), Constraint::DEFAULT_GROUP);

Constraint::DEFAULT_GROUP 等于 'Default'。

英文:

In the example above, I didn't mention that the validation was placed in a specific validation group.
Because of that, the standard constraint wasn't triggered. It was necessary to attach the standard constraint to the default group:

  1. $this->context
  2. ->getValidator()
  3. ->inContext($this->context)
  4. ->validate($value, new Url(), Constraint::DEFAULT_GROUP);

Constraint::DEFAULT_GROUP is equals to 'Default'.

答案2

得分: 0

我无法复现这个问题(Symfony 6.2),所有违规都已添加。也许问题出在其他地方。比较的代码如下:

  1. namespace App\Validator;
  2. use Attribute;
  3. use Symfony\Component\Validator\Constraint;
  4. #[Attribute]
  5. class MyConstraint extends Constraint
  6. {
  7. }
  1. namespace App\Validator;
  2. use Symfony\Component\Validator\Constraint;
  3. use Symfony\Component\Validator\Constraints\Url;
  4. use Symfony\Component\Validator\ConstraintValidator;
  5. class MyConstraintValidator extends ConstraintValidator
  6. {
  7. public function validate($value, Constraint $constraint)
  8. {
  9. $this->context
  10. ->buildViolation('Not valid.')
  11. ->addViolation();
  12. $this->context
  13. ->getValidator()
  14. ->inContext($this->context)
  15. ->validate($value, new Url());
  16. }
  17. }
  1. namespace App\Request;
  2. use App\Validator.MyConstraint;
  3. use Symfony\Component\Validator\Constraints\Email;
  4. use Symfony\Component\Validator\Constraints\Length;
  5. class YourRequest
  6. {
  7. #[Email]
  8. #[MyConstraint]
  9. #[Length(exactly: 2)]
  10. public string $value;
  11. }

在这种情况下,显示了4个违规:Email,'Not valid.',Url,Length。

英文:

I can not reproduce the problem (Symfony 6.2), all violations are added. Perhaps the problem is something else. The code for compare:

  1. namespace App\Validator;
  2. use Attribute;
  3. use Symfony\Component\Validator\Constraint;
  4. #[Attribute]
  5. class MyConstraint extends Constraint
  6. {
  7. }
  1. namespace App\Validator;
  2. use Symfony\Component\Validator\Constraint;
  3. use Symfony\Component\Validator\Constraints\Url;
  4. use Symfony\Component\Validator\ConstraintValidator;
  5. class MyConstraintValidator extends ConstraintValidator
  6. {
  7. public function validate($value, Constraint $constraint)
  8. {
  9. $this->context
  10. ->buildViolation('Not valid.')
  11. ->addViolation();
  12. $this->context
  13. ->getValidator()
  14. ->inContext($this->context)
  15. ->validate($value, new Url());
  16. }
  17. }
  1. namespace App\Request;
  2. use App\Validator\MyConstraint;
  3. use Symfony\Component\Validator\Constraints\Email;
  4. use Symfony\Component\Validator\Constraints\Length;
  5. class YourRequest
  6. {
  7. #[Email]
  8. #[MyConstraint]
  9. #[Length(exactly: 2)]
  10. public string $value;
  11. }

In this case, 4 violations are displayed: Email, 'Not valid.', Url, Length.

huangapple
  • 本文由 发表于 2023年4月7日 02:47:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75952809.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定