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

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

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:

use Symfony\Component\Validator\Constraints\Url;
use Symfony\Component\Validator\Constraint;

public function validate($value, Constraint $constraint): void
{
    [...]
    $this->context
        ->getValidator()
        ->inContext($this->context)
        ->validate($value, new Url());
}

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 :

use Symfony\Component\Validator\Constraints\Url;
use Symfony\Component\Validator\Constraint;

public function validate($value, Constraint $constraint): void
{
    [...]
    $urlConstraint = new Url();
    $violations = $this->context
        ->getValidator()
        ->validate($value, $urlConstraint);

    if (count($violations) !== 0) {
        $this->context
            ->buildViolation($urlConstraint->message)
            ->addViolation();
    }
}

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:

use Symfony\Component\Validator\Constraints\Url;
use Symfony\Component\Validator\Constraint;

public function validate($value, Constraint $constraint): void
{
    [...]
    $this->context
    	->getValidator()
    	->inContext($this->context)
    	->validate($value, new Url());
}

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 :

use Symfony\Component\Validator\Constraints\Url;
use Symfony\Component\Validator\Constraint;

public function validate($value, Constraint $constraint): void
{
    [...]
    $urlConstraint = new Url();
    $violations = $this->context
		->getValidator()
		->validate($value, $urlConstraint);

	if (count($violations) !== 0) {
		$this->context
			->buildViolation($urlConstraint->message)
			->addViolation();
}

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

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

$this->context
    ->getValidator()
    ->inContext($this->context)
    ->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:

$this->context
	->getValidator()
	->inContext($this->context)
	->validate($value, new Url(), Constraint::DEFAULT_GROUP);

Constraint::DEFAULT_GROUP is equals to 'Default'.

答案2

得分: 0

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

namespace App\Validator;

use Attribute;
use Symfony\Component\Validator\Constraint;

#[Attribute]
class MyConstraint extends Constraint
{
}
namespace App\Validator;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Url;
use Symfony\Component\Validator\ConstraintValidator;

class MyConstraintValidator extends ConstraintValidator
{
    public function validate($value, Constraint $constraint)
    {
        $this->context
            ->buildViolation('Not valid.')
            ->addViolation();

        $this->context
            ->getValidator()
            ->inContext($this->context)
            ->validate($value, new Url());
    }
}

namespace App\Request;

use App\Validator.MyConstraint;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\Length;

class YourRequest
{
    #[Email]
    #[MyConstraint]
    #[Length(exactly: 2)]
    public string $value;
}

在这种情况下,显示了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:

namespace App\Validator;

use Attribute;
use Symfony\Component\Validator\Constraint;

#[Attribute]
class MyConstraint extends Constraint
{
}
namespace App\Validator;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Url;
use Symfony\Component\Validator\ConstraintValidator;

class MyConstraintValidator extends ConstraintValidator
{
    public function validate($value, Constraint $constraint)
    {
        $this->context
            ->buildViolation('Not valid.')
            ->addViolation();

        $this->context
            ->getValidator()
            ->inContext($this->context)
            ->validate($value, new Url());
    }
}

namespace App\Request;

use App\Validator\MyConstraint;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\Length;

class YourRequest
{
    #[Email]
    #[MyConstraint]
    #[Length(exactly: 2)]
    public string $value;
}

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:

确定