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

Commit

Permalink
Merge branch 'hotfix/3922' into develop
Browse files Browse the repository at this point in the history
Close #3922
  • Loading branch information
weierophinney committed Mar 11, 2013
2 parents 27e71d4 + 052728c commit 5da8ff7
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
42 changes: 38 additions & 4 deletions library/Zend/Form/Element/Select.php
Expand Up @@ -46,6 +46,11 @@ class Select extends Element implements InputProviderInterface
*/
protected $validator;

/**
* @var bool
*/
protected $disableInArrayValidator = false;

/**
* Create an empty option (option with label but no value). If set to null, no option is created
*
Expand Down Expand Up @@ -119,6 +124,10 @@ public function setOptions($options)
$this->setEmptyOption($this->options['empty_option']);
}

if (isset($this->options['disable_inarray_validator'])) {
$this->setDisableInArrayValidator($this->options['disable_inarray_validator']);
}

return $this;
}

Expand All @@ -140,6 +149,28 @@ public function setAttribute($key, $value)
return parent::setAttribute($key, $value);
}

/**
* Set the flag to allow for disabling the automatic addition of an InArray validator.
*
* @param bool $disableOption
* @return Select
*/
public function setDisableInArrayValidator($disableOption)
{
$this->disableInArrayValidator = (bool) $disableOption;
return $this;
}

/**
* Get the disable in array validator flag.
*
* @return bool
*/
public function disableInArrayValidator()
{
return $this->disableInArrayValidator;
}

/**
* Set the string for an empty option (can be empty string). If set to null, no option will be added
*
Expand Down Expand Up @@ -169,7 +200,7 @@ public function getEmptyOption()
*/
protected function getValidator()
{
if (null === $this->validator) {
if (null === $this->validator && !$this->disableInArrayValidator()) {
$validator = new InArrayValidator(array(
'haystack' => $this->getValueOptionsValues(),
'strict' => false
Expand Down Expand Up @@ -202,11 +233,14 @@ public function getInputSpecification()
$spec = array(
'name' => $this->getName(),
'required' => true,
'validators' => array(
$this->getValidator()
)
);

if ($validator = $this->getValidator()) {
$spec['validators'] = array(
$validator,
);
}

return $spec;
}

Expand Down
13 changes: 13 additions & 0 deletions tests/ZendTest/Form/Element/SelectTest.php
Expand Up @@ -205,5 +205,18 @@ public function testSetOptionsOptions()
$this->assertEquals(array('baz' => 'foo'), $element->getOption('empty_option'));
}

public function testDisableInputSpecification()
{
$element = new SelectElement();
$element->setValueOptions(array(
'Option 1' => 'option1',
'Option 2' => 'option2',
'Option 3' => 'option3',
));
$element->setDisableInArrayValidator(true);

$inputSpec = $element->getInputSpecification();
$this->assertArrayNotHasKey('validators', $inputSpec);
}

}

0 comments on commit 5da8ff7

Please sign in to comment.