Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Commit

Permalink
Implemented fallback_value option for Input filter
Browse files Browse the repository at this point in the history
  • Loading branch information
marek.nos committed Oct 10, 2012
1 parent 8eff238 commit 1c6d74f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
3 changes: 3 additions & 0 deletions library/Zend/InputFilter/Factory.php
Expand Up @@ -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);
Expand Down
35 changes: 34 additions & 1 deletion library/Zend/InputFilter/Input.php
Expand Up @@ -65,6 +65,11 @@ class Input implements InputInterface
*/
protected $value;

/**
* @var mixed
*/
protected $fallbackValue;

public function __construct($name = null)
{
$this->name = $name;
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -272,6 +301,10 @@ public function getMessages()
return (array) $this->errorMessage;
}

if ($this->getFallbackValue()) {
return array();
}

$validator = $this->getValidatorChain();
return $validator->getMessages();
}
Expand Down

0 comments on commit 1c6d74f

Please sign in to comment.