我已经阅读了 Zend-Framework 2 中 Form-Component 的教程/参考,也许我以某种方式错过了它,所以我在这里问。
我有一个对象被调用Node
并将它绑定到一个表单。我正在使用Zend\Stdlib\Hydrator\ArraySerializable
-Standard-Hydrator。所以我的Node
-object 有两种方法exchangeArray()
和getArrayCopy()
这样的:
class Node
{
public function exchangeArray($data)
{
// Standard-Felder
$this->node_id = (isset($data['node_id'])) ? $data['node_id'] : null;
$this->node_name = (isset($data['node_name'])) ? $data['node_name'] : null;
$this->node_body = (isset($data['node_body'])) ? $data['node_body'] : null;
$this->node_date = (isset($data['node_date'])) ? $data['node_date'] : null;
$this->node_image = (isset($data['node_image'])) ? $data['node_image'] : null;
$this->node_public = (isset($data['node_public'])) ? $data['node_public'] : null;
$this->node_type = (isset($data['node_type'])) ? $data['node_type']:null;
$this->node_route = (isset($data['node_route'])) ? $data['node_route']:null;
}
public function getArrayCopy()
{
return get_object_vars($this);
}
}
在我的控制器中,我有一个editAction()
. 在那里我想修改这个Node
对象的值。所以我正在使用bind
我的表单的 - 方法。我的表单只有字段来修改node_name
和node_body
- 属性。在验证表单并在提交表单后转储Node
-object 之后,node_name
和node_body
-properties 现在包含来自提交表单的值。然而,所有其他字段现在都是空的,即使它们之前包含初始值。
class AdminController extends AbstractActionController
{
public function editAction()
{
// ... more stuff here (getting Node, etc)
// Get Form
$form = $this->_getForm(); // return a \Zend\Form instance
$form->bind($node); // This is the Node-Object; It contains values for every property
if(true === $this->request->isPost())
{
$data = $this->request->getPost();
$form->setData($data);
// Check if form is valid
if(true === $form->isValid())
{
// Dumping here....
// Here the Node-object only contains values for node_name and node_body all other properties are empty
echo'<pre>';print_r($node);echo'</pre>';exit;
}
}
// View
return array(
'form' => $form,
'node' => $node,
'nodetype' => $nodetype
);
}
}
我只想覆盖来自表单 (node_name
和node_body
) 而不是其他的值。他们应该保持不变。
我认为一个可能的解决方案是将其他属性作为隐藏字段提供给表单,但是我不想这样做。
是否有可能不覆盖表单中不存在的值?