需要找到一种在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();
}
}
}
答案 0 :(得分:2)
好的,我找到了一个解决方案,我将提供解决方案:
验证者:
<?php
namespace App\Validator\Constraints;
use App\Model\Odx2Parameter;
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($parameter, Constraint $constraint)
{
if (!$constraint instanceof UdsMessage) {
throw new UnexpectedTypeException($constraint, UdsMessage::class);
}
if (!($parameter instanceof Odx2Parameter)) {
throw new UnexpectedValueException($parameter, Odx2Parameter::class);
}
$udsId = $parameter->getUdsId();
$startBit = $parameter->getStartBit();
$stopBit = $parameter->getStopBit();
if ($startBit <= $stopBit && $stopBit <= ($udsId * 1024)) {
return true;
} else {
$this->context->buildViolation($constraint->message)
->setParameter('{{ udsId }}', $udsId)
->atPath('udsId')
->addViolation();
$this->context->buildViolation($constraint->startBitMessage)
->setParameter('{{ startBit }}', $startBit)
->setParameter('{{ stopBit }}', $stopBit)
->atPath('startBit')
->addViolation();
$this->context->buildViolation($constraint->stopBitMessage)
->setParameter('{{ stopBit }}', $stopBit)
->setParameter('{{ stopBit }}', $stopBit)
->atPath('stopBit')
->addViolation();
}
}
}
约束:
<?php
namespace App\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
*/
class UdsMessage extends Constraint
{
public $message = 'The UDS "{{ udsId }}" contains an illegal startBit or stopBit value.';
public $startBitMessage = '';
public $stopBitMessage = '';
public function getTargets()
{
return self::CLASS_CONSTRAINT;
}
}
FormType:
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'constraints' => [
New UdsMessage()
]
]);
}