需要找到一种方法,在Symfony中使用自定义验证器根据其他值验证字段。在底部,我提供了我们如何在旧应用程序中进行验证的方式,但是在Symfony中如何获取另一个字段的值呢?
需要一些帮助,各位,我有自定义验证器,这不是问题,但问题是-如何获取另一个字段的值。
需要一些帮助,各位,我有自定义验证器,这不是问题,但问题是-如何获取另一个字段的值。
private function validate_uds_message($size, $start, $stop) {
return $start <= $stop && $stop <= ($size * 1024) ? true : false;
}
验证器:
<?php
namespace App\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;
class UdsMessageValidator extends ConstraintValidator
{
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof UdsMessage) {
throw new UnexpectedTypeException($constraint, UdsMessage::class);
}
if (null === $value || '' === $value) {
return;
}
if (!is_int($value)) {
throw new UnexpectedValueException($value, 'int');
}
$startBit = /* ... */;
$stopBit = /* ... */;
if ($startBit <= $stopBit && $stopBit <= ($value * 1024)) {
return true;
} else {
$this->context->buildViolation($constraint->message)
->setParameter('{{ udsId }}', $value)
->addViolation();
}
}
}