diff --git a/library/Zend/InputFilter/Factory.php b/library/Zend/InputFilter/Factory.php index 0deb56b2873..f4d15cac5f8 100644 --- a/library/Zend/InputFilter/Factory.php +++ b/library/Zend/InputFilter/Factory.php @@ -158,6 +158,9 @@ public function createInput($inputSpecification) $input->setRequired(!$value); } break; + case 'fallback_value': + $input->setFallbackValue($value); + break; case 'filters': if ($value instanceof FilterChain) { $input->setFilterChain($value); diff --git a/library/Zend/InputFilter/Input.php b/library/Zend/InputFilter/Input.php index 9985b795791..42390c959a6 100644 --- a/library/Zend/InputFilter/Input.php +++ b/library/Zend/InputFilter/Input.php @@ -65,6 +65,11 @@ class Input implements InputInterface */ protected $value; + /** + * @var mixed + */ + protected $fallbackValue; + public function __construct($name = null) { $this->name = $name; @@ -151,6 +156,16 @@ public function setValue($value) return $this; } + /** + * @param mixed $value + * @return Input + */ + public function setFallbackValue($value) + { + $this->fallbackValue = $value; + return $this; + } + /** * @return boolean */ @@ -230,6 +245,14 @@ public function getValue() return $filter->filter($this->value); } + /** + * @return mixed + */ + public function getFallbackValue() + { + return $this->fallbackValue; + } + /** * @param InputInterface $input * @return Input @@ -260,7 +283,13 @@ public function isValid($context = null) $this->injectNotEmptyValidator(); $validator = $this->getValidatorChain(); $value = $this->getValue(); - return $validator->isValid($value, $context); + $result = $validator->isValid($value, $context); + if (!$result && $fallbackValue = $this->getFallbackValue()) { + $this->setValue($fallbackValue); + $result = true; + } + + return $result; } /** @@ -272,6 +301,10 @@ public function getMessages() return (array) $this->errorMessage; } + if ($this->getFallbackValue()) { + return array(); + } + $validator = $this->getValidatorChain(); return $validator->getMessages(); }