0

I have a case for you. I have an entity that contains two integers. Both are between 0 and 100, but one is the minimum value and one is the maximum value. Therefore, the max value should be greater than or equal to the min value.

I have had a look at http://symfony.com/doc/current/book/validation.html and http://symfony.com/doc/current/cookbook/validation/custom_constraint.html

In the last of these links we have the following example:

// src/Acme/DemoBundle/Validator/Constraints/ContainsAlphanumericValidator.php
namespace Acme\DemoBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

class ContainsAlphanumericValidator extends ConstraintValidator
{
    public function validate($value, Constraint $constraint)
    {
        if (!preg_match('/^[a-zA-Za0-9]+$/', $value, $matches)) {
            $this->context->addViolation($constraint->message, array('%string%' => $value));
        }
    }
}

But this takes the $value, and I need to compare the min and max to each other. How do I reference the "min" value inside the "max" validator?

1 Answer 1

3

You could use the callback constraint to use your own logic to compare the values and add a violation if needed. See http://symfony.com/doc/current/reference/constraints/Callback.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.