ZF2表单在执行JQuery后无效,在haystack中找不到获取输入的信息
我有一个使用数组表示法创建的表单。我有两个下拉列表。一个是父级,第二个是所选父级的同级。我正在使用JQuery更新父级更改时的同级列表 我遇到的问题是,在JQuery替换同级列表中的原始项之后,表单无法验证。我想在进行验证之前将新的兄弟姐妹列表推送到表单中。我的代码如下所示:ZF2表单在执行JQuery后无效,在haystack中找不到获取输入的信息,jquery,zend-framework2,Jquery,Zend Framework2,我有一个使用数组表示法创建的表单。我有两个下拉列表。一个是父级,第二个是所选父级的同级。我正在使用JQuery更新父级更改时的同级列表 我遇到的问题是,在JQuery替换同级列表中的原始项之后,表单无法验证。我想在进行验证之前将新的兄弟姐妹列表推送到表单中。我的代码如下所示: public function GetMenuChildrenAction() { $request = $this->getRequest(); $response = $this->getR
public function GetMenuChildrenAction()
{
$request = $this->getRequest();
$response = $this->getResponse();
if ($request->isPost())
{
$post_data = $request->getPost();
$parent_id = $post_data['pID'];
$id = $post_data['thisID'];
$sm = $this->getServiceLocator();
$tg = $sm->get('MenuTableGateway');
$newOptions = $this->getMenuTable()->AjaxGetPositionsByID($parent_id, $id, $tg);
$this->newOptions = $newOptions;
//$x = json_encode($newOptions);
if(isset($newOptions))
{
$response->setContent(\Zend\Json\Json::encode(array('response' => true, 'newOptions' => $newOptions)));
}
else
{
$response->setContent(\Zend\Json\Json::encode(array('response' => false)));
}
}
return $response;
}
这是jQuery方法:
jQuery(function($) {
$("#parent").change(function(event){
event.preventDefault();
var parentID = $('#parent').val();
var id = $('#id').val();
$.post("**/menu/GetMenuChildren**", {pID: parentID, thisID: id },
function(data){
if(data.response === true){
var $el = $("#Position");
$el.empty(); // remove old options
$.each(data.newOptions, function(value,key) {
$el.append($("<option></option>")
.attr("value", value).text(key));
});
} else {
// print error message
alert("something wrong in Post");
}
}, 'json');
});
});
单击表单上的“保存”按钮时,将执行编辑操作,如下所示:
public function editAction()
{
$id = (int) $this->params()->fromRoute('id',0);
if(!$id) {
return $this->redirect()->toRoute('menu',array('action' => 'insert'));
}
try
{
$menu = $this->getMenuTable()->getMenu($id);
}
catch (\Exception $ex) {
return $this->redirect()->toRoute('menu', array('action' => 'index'));
}
if(!$this->form)
{
$this->form = new MenuForm($this->getMenuTable(), $this->getServiceLocator());
}
$this->form->getInputFilter();
$this->form->bind($menu);
$this->form->get('submit')->setAttribute('value','Save');
$sm = $this->getServiceLocator();
$tg = $sm->get('MenuTableGateway');
$request = $this->getRequest();
if($request->isPost()) {
$this->form->setInputFilter($menu->getInputFilter());
$this->form->setData($request->getPost());
if($this->form->isValid())
{
$this->getMenuTable()->saveMenu($this->form->getData());
return $this->redirect()->toRoute('menu');
}
}
$sm = $this->getServiceLocator();
$tg = $sm->get('MenuTableGateway');
$mData = new \FoyCam\MenuData($tg);
$menuData = $mData->GetEditMenuData();
$this->menuTree = new \FoyCam\MenuTree($menuData);
return array(
'id' => $id,
'form' => $this->form,
'menuTree' => $this->menuTree,
);
}
我尝试输入以下内容,newOptions是受保护的类成员,在调用getMenuChildren方法时存储,但newOptions为null,我猜jQuery使用了表单的新实例
$this->form->get("position")->setValueOptions($this->newOptions);
$this->form->setData($request->getPost());
谢谢你的帮助。我怎样才能使表单生效?同样,我得到的输入在haystack中找不到我假设你有一个通过JS填充的选择列表?在验证表单之前,您需要将所有有效的可能性传递到Select元素中,因为SelectElement始终会附加一个inaray验证器