Symfony中如何验证依赖于另一个字段的字段

3
需要找到一种方法,在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();
        }
    }
}

你需要尝试使用Class Constraint Validator - habibun
1个回答

6

好的,我找到了如何解决这个问题,我将提供我的解决方案:

验证器:

<?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()
        ]
    ]);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接