Largo
Largo

Reputation: 159

How to validate field that depend on another one in Symfony

Need to find a way how in Symfony in custom validators validate fields based on another values. At the bottom, I've provided how we validate in legacy application, but how can we get values from another field in Symfony?

I have custom validators and that's not a problem, but the problem is - how to get values of another fields.

private function validate_uds_message($size, $start, $stop) {
    return $start <= $stop && $stop <= ($size * 1024) ? true : false;
}

Validator:

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

Upvotes: 3

Views: 2810

Answers (1)

Largo
Largo

Reputation: 159

Ok, I've found a solution how to work with it, I'll provide my solution:

Validator:

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

Constraint:

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

Upvotes: 6

Related Questions