NotBlankValidator.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. * @author Kévin Dunglas <dunglas@gmail.com>
  17. */
  18. class NotBlankValidator extends ConstraintValidator
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public function validate($value, Constraint $constraint)
  24. {
  25. if (!$constraint instanceof NotBlank) {
  26. throw new UnexpectedTypeException($constraint, NotBlank::class);
  27. }
  28. if ($constraint->allowNull && null === $value) {
  29. return;
  30. }
  31. if (\is_string($value) && null !== $constraint->normalizer) {
  32. $value = ($constraint->normalizer)($value);
  33. }
  34. if (false === $value || (empty($value) && '0' != $value)) {
  35. $this->context->buildViolation($constraint->message)
  36. ->setParameter('{{ value }}', $this->formatValue($value))
  37. ->setCode(NotBlank::IS_BLANK_ERROR)
  38. ->addViolation();
  39. }
  40. }
  41. }