Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Trying to solve issue ZF2-558 #2393

Closed
wants to merge 7 commits into from

4 participants

@igormx

The setValueOptions method of Zend/Form/Element/Select and Zend/Form/Element/Select were updating the options of the Element but were not updating the InArray Validator Haystack if the options were added after that the validator were attached.

@cgmartin

Needs CS style fixes with the indentation (4 spaces). Looks good otherwise IMO.

@igormx

Done, i have fix the indentation. TY

@weierophinney weierophinney referenced this pull request from a commit
@weierophinney weierophinney [#2393][ZF2-558] CS fixes
- indentation
- trailing spaces
07c7e6c
@timdev

I think this is a bad solution (or I'm missing something). There's no guarantee that if $this->validator is not an instance of InArray that whatever it is implements a getValidator() method. It's not clear to me what $this->validator is expected to be if the instanceof test fails (which is why I'm not providing an alternative solution).

For instance, this change appears to break DoctrineORMModule\Form\Element\DoctrineEntity, which overrides Select::getValidator().

Aha! This patch assumes that $this->validator is either an instanceof InArray or Explode. I've fixed my problem for now by simply subclassing DoctrineEntity and overriding setValueOptions() to behave like it did before this commit. I suppose I could put a pull request in for DoctrineORMModule to do the same there, which would fix the issue. But perhaps there's a more elegant solution at this level.

I've made a pull request that I think resolves the issue.

@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney [#2393][ZF2-558] CS fixes
- indentation
- trailing spaces
a792560
@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/zf2-558'
Close #2393
284985f
@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/zf2-558' into develop
Forward port #2393
37986cf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 19, 2012
  1. @igormx

    Trying to solve the issue ZF2-558 with the solution provided by Chris

    igormx authored
    Willis, if you set the options values and you have InArray Validator the
    patch update the validator too
Commits on Sep 20, 2012
  1. Created unit test for the change

    igor authored
  2. Adding the Unit testing method for the modification on

    igor authored
    Zend/Form/Element/MultiCheckbox
Commits on Sep 21, 2012
  1. fixing Coding Standards for the added code

    igor authored
This page is out of date. Refresh to see the latest.
View
7 library/Zend/Form/Element/MultiCheckbox.php
@@ -62,6 +62,13 @@ public function getValueOptions()
public function setValueOptions(array $options)
{
$this->valueOptions = $options;
+
+ // Update InArray validator haystack
+ if (!is_null($this->validator)) {
+ $validator = $this->validator->getValidator();
+ $validator->setHaystack($this->getValueOptionsValues());
+ }
+
return $this;
}
View
7 library/Zend/Form/Element/Select.php
@@ -80,6 +80,13 @@ public function getValueOptions()
public function setValueOptions(array $options)
{
$this->valueOptions = $options;
+
+ // Update InArrayValidator validator haystack
+ if (!is_null($this->validator)) {
+ $validator = $this->validator instanceof InArrayValidator ? $this->validator : $this->validator->getValidator();
+ $validator->setHaystack($this->getValueOptionsValues());
+ }
+
return $this;
}
View
20 tests/ZendTest/Form/Element/MultiCheckboxTest.php
@@ -105,6 +105,26 @@ public function testInArrayValidationOfOptions($valueTests, $options)
$this->assertTrue($explodeValidator->isValid($valueTests));
}
+ /**
+ * Testing that InArray Validator Haystack is Updated if the Options
+ * are added after the validator is attached
+ *
+ * @dataProvider multiCheckboxOptionsDataProvider
+ */
+ public function testInArrayValidatorHaystakIsUpdated($valueTests, $options)
+ {
+ $element = new MultiCheckboxElement('my-checkbox');
+ $inputSpec = $element->getInputSpecification();
+ $inArrayValidator=$inputSpec['validators'][0]->getValidator();
+
+ $element->setAttributes(array(
+ 'options' => $options,
+ ));
+ $haystack=$inArrayValidator->getHaystack();
+ $this->assertCount(count($options), $haystack);
+ }
+
+
public function testAttributeType()
{
$element = new MultiCheckboxElement();
View
20 tests/ZendTest/Form/Element/SelectTest.php
@@ -151,6 +151,26 @@ public function testInArrayValidationOfOptions($valueTests, $options)
$this->assertTrue($inArrayValidator->isValid($valueToTest));
}
}
+
+ /**
+ * Testing that InArray Validator Haystack is Updated if the Options
+ * are added after the validator is attached
+ *
+ * @dataProvider selectOptionsDataProvider
+ */
+ public function testInArrayValidatorHaystakIsUpdated($valueTests, $options)
+ {
+ $element = new SelectElement('my-select');
+ $inputSpec = $element->getInputSpecification();
+
+ $inArrayValidator = $inputSpec['validators'][0];
+ $this->assertInstanceOf('Zend\Validator\InArray', $inArrayValidator);
+
+ $element->setValueOptions($options);
+ $haystack=$inArrayValidator->getHaystack();
+ $this->assertCount(count($options), $haystack);
+ }
+
public function testOptionsHasArrayOnConstruct()
{
Something went wrong with that request. Please try again.