PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/sfFormExtraPlugin/lib/validator/sfValidatorDefault.class.php

https://bitbucket.org/Kudlaty/360kdw
PHP | 76 lines | 29 code | 6 blank | 41 comment | 1 complexity | 122f99df59284f63338fd9a1e055e4ff MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * Returns a default value instead of throwing an error on validation failure.
  11. *
  12. * $this->validatorSchema['sort'] = new sfValidatorDefault(array(
  13. * 'validator' => new sfValidatorChoice(array('choices' => array('up', 'down'))),
  14. * 'default' => 'up',
  15. * ));
  16. *
  17. * If no default option is provided, the supplied validator's empty value will
  18. * be returned on error.
  19. *
  20. * @package sfFormExtraPlugin
  21. * @subpackage validator
  22. * @author Kris Wallsmith <kris.wallsmith@symfony-project.com>
  23. * @version SVN: $Id: sfValidatorDefault.class.php 27625 2010-02-06 22:07:40Z Kris.Wallsmith $
  24. */
  25. class sfValidatorDefault extends sfValidatorBase
  26. {
  27. /**
  28. * Configures the current validator.
  29. *
  30. * Available options:
  31. *
  32. * * validator: The validator to use
  33. * * default: The value to return if the validator fails
  34. *
  35. * @see sfValidatorBase
  36. */
  37. protected function configure($options = array(), $messages = array())
  38. {
  39. $this->addRequiredOption('validator');
  40. $this->addOption('default', null);
  41. }
  42. /**
  43. * @see sfValidatorBase
  44. */
  45. protected function isEmpty($value)
  46. {
  47. return false;
  48. }
  49. /**
  50. * @see sfValidatorBase
  51. *
  52. * @throws InvalidArgumentException If the validator option is not a validator object
  53. */
  54. protected function doClean($value)
  55. {
  56. $validator = $this->getOption('validator');
  57. if (!$validator instanceof sfValidatorBase)
  58. {
  59. throw new InvalidArgumentException('The "validator" option must be an instance of sfValidatorBase.');
  60. }
  61. try
  62. {
  63. return $validator->clean($value);
  64. }
  65. catch (sfValidatorError $error)
  66. {
  67. return null === $this->getOption('default') ? $validator->getEmptyValue() : $this->getOption('default');
  68. }
  69. }
  70. }