BlankValidator.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Validator\Constraints;
  11. use Symfony\Component\Validator\Constraint;
  12. use Symfony\Component\Validator\ConstraintValidator;
  13. use Symfony\Component\Validator\Exception\UnexpectedTypeException;
  14. /**
  15. * @author Bernhard Schussek <bschussek@gmail.com>
  16. */
  17. class BlankValidator extends ConstraintValidator
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public function validate($value, Constraint $constraint)
  23. {
  24. if (!$constraint instanceof Blank) {
  25. throw new UnexpectedTypeException($constraint, Blank::class);
  26. }
  27. if ('' !== $value && null !== $value) {
  28. $this->context->buildViolation($constraint->message)
  29. ->setParameter('{{ value }}', $this->formatValue($value))
  30. ->setCode(Blank::NOT_BLANK_ERROR)
  31. ->addViolation();
  32. }
  33. }
  34. }