如何在Symfony中验证依赖于另一个字段的字段
需要找到一种方法,如何在自定义验证器的Symfony中基于其他值验证字段。最后,我提供了如何在遗留应用程序中进行验证,但如何从Symfony的另一个字段中获取值 需要一些帮助的家伙,我有自定义验证器,这不是问题,但问题是-如何获得其他字段的值如何在Symfony中验证依赖于另一个字段的字段,symfony,validation,Symfony,Validation,需要找到一种方法,如何在自定义验证器的Symfony中基于其他值验证字段。最后,我提供了如何在遗留应用程序中进行验证,但如何从Symfony的另一个字段中获取值 需要一些帮助的家伙,我有自定义验证器,这不是问题,但问题是-如何获得其他字段的值 private function validate_uds_message($size, $start, $stop) { return $start <= $stop && $stop <= ($size * 1024
private function validate_uds_message($size, $start, $stop) {
return $start <= $stop && $stop <= ($size * 1024) ? true : false;
}
private函数验证消息($size、$start、$stop){
return$start好的,我已经找到了一个解决方案如何使用它,我将提供我的解决方案:
验证器:
<?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();
}
}
}
<?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;
}
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'constraints' => [
New UdsMessage()
]
]);
}