Permalink
Browse files

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

Willis, if you set the options values and you have InArray Validator the
patch update the validator too
  • Loading branch information...
1 parent 836249f commit 294a7d18a832ce5eb297b43832c2b67db77fb425 @igormx igormx committed Sep 19, 2012
Showing with 14 additions and 0 deletions.
  1. +7 −0 library/Zend/Form/Element/MultiCheckbox.php
  2. +7 −0 library/Zend/Form/Element/Select.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;
}
@@ -80,6 +80,13 @@ public function getValueOptions()
public function setValueOptions(array $options)
{
$this->valueOptions = $options;
+
+ // Update InArray validator haystack
+ if (!is_null($this->validator)) {
+ $validator = $this->validator instanceof InArray ? $this->validator : $this->validator->getValidator();
+ $validator->setHaystack($this->getValueOptionsValues());
+ }
+
return $this;
}

3 comments on commit 294a7d1

@timdev
timdev commented on 294a7d1 Sep 27, 2012

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().

@timdev
timdev commented on 294a7d1 Sep 27, 2012

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.

@timdev
timdev commented on 294a7d1 Sep 27, 2012

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

Please sign in to comment.