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

Commit

Permalink
Merge branch 'fix/form-not-empty-validator-messages' of https://githu…
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 1 deletion.
119 changes: 118 additions & 1 deletion src/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,92 +12,172 @@

use Zend\Filter\FilterChain;
use Zend\Validator\ValidatorChain;
use Zend\Validator\NotEmpty;

/**
* @category Zend
* @package Zend_InputFilter
*/
class Input implements InputInterface
{
/**
* @var boolean
*/
protected $allowEmpty = false;

/**
* @var boolean
*/
protected $breakOnFailure = false;

/**
* @var string|null
*/
protected $errorMessage;

/**
* @var FilterChain
*/
protected $filterChain;

/**
* @var string
*/
protected $name;

/**
* @var boolean
*/
protected $notEmptyValidator = false;

/**
* @var boolean
*/
protected $required = true;

/**
* @var ValidatorChain
*/
protected $validatorChain;

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

public function __construct($name = null)
{
$this->name = $name;
}

/**
* @param boolean $allowEmpty
* @return Input
*/
public function setAllowEmpty($allowEmpty)
{
$this->allowEmpty = (bool) $allowEmpty;
return $this;
}

/**
* @param boolean $breakOnFailure
* @return Input
*/
public function setBreakOnFailure($breakOnFailure)
{
$this->breakOnFailure = (bool) $breakOnFailure;
return $this;
}

/**
* @param string|null $errorMessage
* @return Input
*/
public function setErrorMessage($errorMessage)
{
$errorMessage = (null === $errorMessage) ? null : (string) $errorMessage;
$this->errorMessage = $errorMessage;
return $this;
}

/**
* @param FilterChain $filterChain
* @return Input
*/
public function setFilterChain(FilterChain $filterChain)
{
$this->filterChain = $filterChain;
return $this;
}

/**
* @param string $name
* @return Input
*/
public function setName($name)
{
$this->name = (string) $name;
return $this;
}

/**
* @param boolean $required
* @return Input
*/
public function setRequired($required)
{
$this->required = (bool) $required;
return $this;
}

/**
* @param ValidatorChain $validatorChain
* @return Input
*/
public function setValidatorChain(ValidatorChain $validatorChain)
{
$this->validatorChain = $validatorChain;
return $this;
}

/**
* @param mixed $value
* @return Input
*/
public function setValue($value)
{
$this->value = $value;
return $this;
}

/**
* @return boolean
*/
public function allowEmpty()
{
return $this->allowEmpty;
}

/**
* @return boolean
*/
public function breakOnFailure()
{
return $this->breakOnFailure;
}

/**
* @return string|null
*/
public function getErrorMessage()
{
return $this->errorMessage;
}

/**
* @return FilterChain
*/
public function getFilterChain()
{
if (!$this->filterChain) {
Expand All @@ -106,21 +186,33 @@ public function getFilterChain()
return $this->filterChain;
}

/**
* @return string
*/
public function getName()
{
return $this->name;
}

/**
* @return mixed
*/
public function getRawValue()
{
return $this->value;
}

/**
* @return boolean
*/
public function isRequired()
{
return $this->required;
}

/**
* @return ValidatorChain
*/
public function getValidatorChain()
{
if (!$this->validatorChain) {
Expand All @@ -129,12 +221,19 @@ public function getValidatorChain()
return $this->validatorChain;
}

/**
* @return mixed
*/
public function getValue()
{
$filter = $this->getFilterChain();
return $filter->filter($this->value);
}

/**
* @param InputInterface $input
* @return Input
*/
public function merge(InputInterface $input)
{
$this->setAllowEmpty($input->allowEmpty());
Expand All @@ -149,8 +248,13 @@ public function merge(InputInterface $input)

$validatorChain = $input->getValidatorChain();
$this->getValidatorChain()->merge($validatorChain);
return $this;
}

/**
* @param mixed $context Extra "context" to provide the validator
* @return boolean
*/
public function isValid($context = null)
{
$this->injectNotEmptyValidator();
Expand All @@ -159,6 +263,9 @@ public function isValid($context = null)
return $validator->isValid($value, $context);
}

/**
* @return array
*/
public function getMessages()
{
if (null !== $this->errorMessage) {
Expand All @@ -171,10 +278,20 @@ public function getMessages()

protected function injectNotEmptyValidator()
{
if (!$this->isRequired() && $this->allowEmpty() && !$this->notEmptyValidator) {
if ((!$this->isRequired() && $this->allowEmpty()) || $this->notEmptyValidator) {
return;
}
$chain = $this->getValidatorChain();

// Check if NotEmpty validator is already first in chain
$validators = $chain->getValidators();
if (isset($validators[0]['instance'])
&& $validators[0]['instance'] instanceof NotEmpty
) {
$this->notEmptyValidator = true;
return;
}

$chain->prependByName('NotEmpty', array(), true);
$this->notEmptyValidator = true;
}
Expand Down
28 changes: 28 additions & 0 deletions test/InputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,36 @@ public function testNotEmptyValidatorAddedWhenIsValidIsCalled()
$input = new Input('foo');
$this->assertTrue($input->isRequired());
$input->setValue('');
$validatorChain = $input->getValidatorChain();
$this->assertEquals(0, count($validatorChain->getValidators()));

$this->assertFalse($input->isValid());
$messages = $input->getMessages();
$this->assertArrayHasKey('isEmpty', $messages);
$this->assertEquals(1, count($validatorChain->getValidators()));

// Assert that NotEmpty validator wasn't added again
$this->assertFalse($input->isValid());
$this->assertEquals(1, count($validatorChain->getValidators()));
}

public function testRequiredNotEmptyValidatorNotAddedWhenOneExists()
{
$input = new Input('foo');
$this->assertTrue($input->isRequired());
$input->setValue('');

$notEmptyMock = $this->getMock('Zend\Validator\NotEmpty', array('isValid'));
$notEmptyMock->expects($this->exactly(1))
->method('isValid')
->will($this->returnValue(false));

$validatorChain = $input->getValidatorChain();
$validatorChain->prependValidator($notEmptyMock);
$this->assertFalse($input->isValid());

$validators = $validatorChain->getValidators();
$this->assertEquals(1, count($validators));
$this->assertEquals($notEmptyMock, $validators[0]['instance']);
}
}

0 comments on commit 38a8855

Please # to comment.