Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Allow form element filter to convert a string to array #3819

Closed
wants to merge 1 commit into from

2 participants

@davidwindell

No description provided.

@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/3819'
Close #3819
4f51c1d
@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/3819' into develop
Forward port #3819
e59269b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 19, 2013
  1. @davidwindell
This page is out of date. Refresh to see the latest.
View
2  library/Zend/Form/Form.php
@@ -321,7 +321,7 @@ protected function prepareBindData(array $values, array $match)
continue;
}
- if (is_array($value)) {
+ if (is_array($value) && is_array($match[$name])) {
$data[$name] = $this->prepareBindData($value, $match[$name]);
} else {
$data[$name] = $value;
View
21 tests/ZendTest/Form/FormTest.php
@@ -1292,4 +1292,25 @@ public function testCorrectlyHydrateBaseFieldsetWhenHydratorThatDoesNotIgnoreInv
// Make sure the object was not hydrated at the "form level"
$this->assertFalse(isset($object->submit));
}
+
+ public function testPrepareBindDataAllowsFilterToConvertStringToArray()
+ {
+ $data = array(
+ 'foo' => '1,2',
+ );
+
+ $filteredData = array(
+ 'foo' => array(1, 2)
+ );
+
+ $element = new TestAsset\ElementWithStringToArrayFilter('foo');
+ $hydrator = $this->getMock('Zend\Stdlib\Hydrator\ArraySerializable');
+ $hydrator->expects($this->any())->method('hydrate')->with($filteredData, $this->anything());
+
+ $this->form->add($element);
+ $this->form->setHydrator($hydrator);
+ $this->form->setObject(new stdClass());
+ $this->form->setData($data);
+ $this->form->bindValues($data);
+ }
}
View
27 tests/ZendTest/Form/TestAsset/ElementWithStringToArrayFilter.php
@@ -0,0 +1,27 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+
+namespace ZendTest\Form\TestAsset;
+
+use Zend\Form\Element;
+use Zend\InputFilter\InputProviderInterface;
+
+class ElementWithStringToArrayFilter extends Element implements InputProviderInterface
+{
+ public function getInputSpecification()
+ {
+ return array(
+ 'name' => $this->getName(),
+ 'required' => true,
+ 'filters' => array(
+ array('name' => 'ZendTest\Form\TestAsset\StringToArrayFilter'),
+ ),
+ );
+ }
+}
View
23 tests/ZendTest/Form/TestAsset/StringToArrayFilter.php
@@ -0,0 +1,23 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+
+namespace ZendTest\Form\TestAsset;
+
+use Zend\Filter\AbstractFilter;
+
+class StringToArrayFilter extends AbstractFilter
+{
+ public function filter($value)
+ {
+ if (!is_array($value)) {
+ return explode(',', $value);
+ }
+ return $value;
+ }
+}
Something went wrong with that request. Please try again.