ValidatorExtension.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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\Form\Extension\Validator;
  11. use Symfony\Component\Form\AbstractExtension;
  12. use Symfony\Component\Form\Exception\UnexpectedTypeException;
  13. use Symfony\Component\Form\Extension\Validator\Constraints\Form;
  14. use Symfony\Component\Validator\Constraints\Valid;
  15. use Symfony\Component\Validator\Mapping\ClassMetadata;
  16. use Symfony\Component\Validator\Validator\ValidatorInterface;
  17. use Symfony\Component\Validator\ValidatorInterface as LegacyValidatorInterface;
  18. /**
  19. * Extension supporting the Symfony Validator component in forms.
  20. *
  21. * @author Bernhard Schussek <bschussek@gmail.com>
  22. */
  23. class ValidatorExtension extends AbstractExtension
  24. {
  25. private $validator;
  26. /**
  27. * @param ValidatorInterface|LegacyValidatorInterface $validator
  28. *
  29. * @throws UnexpectedTypeException If $validator is invalid
  30. */
  31. public function __construct($validator)
  32. {
  33. // 2.5 API
  34. if ($validator instanceof ValidatorInterface) {
  35. $metadata = $validator->getMetadataFor('Symfony\Component\Form\Form');
  36. // 2.4 API
  37. } elseif ($validator instanceof LegacyValidatorInterface) {
  38. @trigger_error('Passing an instance of Symfony\Component\Validator\ValidatorInterface as argument to the '.__METHOD__.' method is deprecated since Symfony 2.8 and will be removed in 3.0. Use an implementation of Symfony\Component\Validator\Validator\ValidatorInterface instead', E_USER_DEPRECATED);
  39. $metadata = $validator->getMetadataFactory()->getMetadataFor('Symfony\Component\Form\Form');
  40. } else {
  41. throw new UnexpectedTypeException($validator, 'Symfony\Component\Validator\Validator\ValidatorInterface or Symfony\Component\Validator\ValidatorInterface');
  42. }
  43. // Register the form constraints in the validator programmatically.
  44. // This functionality is required when using the Form component without
  45. // the DIC, where the XML file is loaded automatically. Thus the following
  46. // code must be kept synchronized with validation.xml
  47. /* @var $metadata ClassMetadata */
  48. $metadata->addConstraint(new Form());
  49. $metadata->addPropertyConstraint('children', new Valid());
  50. $this->validator = $validator;
  51. }
  52. public function loadTypeGuesser()
  53. {
  54. // 2.5 API
  55. if ($this->validator instanceof ValidatorInterface) {
  56. return new ValidatorTypeGuesser($this->validator);
  57. }
  58. // 2.4 API
  59. return new ValidatorTypeGuesser($this->validator->getMetadataFactory());
  60. }
  61. protected function loadTypeExtensions()
  62. {
  63. return array(
  64. new Type\FormTypeValidatorExtension($this->validator),
  65. new Type\RepeatedTypeValidatorExtension(),
  66. new Type\SubmitTypeValidatorExtension(),
  67. );
  68. }
  69. }