的实际问题是,Zend的形式不与尚未发送的值理会,并在排空收集的情况下,您只需将不会发送任何数据,这是为什么表格忽略集合,导致表单和/或其字段集不告诉他们的水化器对集合进行任何更改。
最终,您可以责怪this函数,该函数删除从传递给“setData”的数组中未表示的表单中提取的过滤数据。
我管理通过重写形式的“使用setData”功能附加地处理所传递的数据,以包括空数组为仍处于字段集集合来解决这个问题,但在数据阵列中未表示:
namespace Module\Form;
class Form extends \Zend\Form\Form
{
/**
* Fill the passed data array with placeholder arrays for collections
* existing in the passed fieldset (and its sub-fieldsets) but not
* represented in the data array.
*
* @param \Zend\Form\FieldsetInterface $fieldset
* @param array $data
* @return array
*/
protected static function assertCollectionPlaceholders(\Zend\Form\FieldsetInterface $fieldset, array $data)
{
foreach ($fieldset as $name => $elementOrFieldset) {
if (!$elementOrFieldset instanceof \Zend\Form\FieldsetInterface) {
continue;
}
if (array_key_exists($name, $data) && is_array($data[$name])) {
$data[$name] = static::assertCollectionPlaceholders($elementOrFieldset, $data[$name]);
} else if ($elementOrFieldset instanceof \Zend\Form\Element\Collection) {
$data[$name] = array();
}
}
return $data;
}
/**
* Set data to validate and/or populate elements
*
* Typically, also passes data on to the composed input filter.
*
* @see \Zend\Form\Form
* @param array|\ArrayAccess|Traversable $data
* @return self
* @throws \Zend\Form\Exception\InvalidArgumentException
*/
public function setData($data)
{
if ($data instanceof \Traversable) {
$data = \Zend\Stdlib\ArrayUtils::iteratorToArray($data);
}
if (!is_array($data)) {
throw new \Zend\Form\Exception\InvalidArgumentException(sprintf(
'%s expects an array or Traversable argument; received "%s"',
__METHOD__,
(is_object($data) ? get_class($data) : gettype($data))
));
}
$data = static::assertCollectionPlaceholders($this, $data);
$this->hasValidated = false;
$this->data = $data;
$this->populateValues($data);
return $this;
}
}
通过这样做,表单和/或其字段集告诉他们水化器集合是空的,并且在Doctrine的水化器的情况下,提示他们移除集合中没有的元素。