在JQuery执行后,ZF2表格无效,获取在haystack中找不到输入

时间:2013-05-06 12:57:11

标签: jquery zend-framework2

我有一个使用数组表示法创建的表单。我有两个下拉列表。一个是Parent,第二个是所选父级的兄弟。我正在使用JQuery在更改父级时更新兄弟列表。

我遇到的问题是,在JQuery替换兄弟列表中的原始项目后,表单无法验证。我希望在验证发生之前将新的兄弟列表推送到表单中。我的代码如下所示:

这是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');
    });
});

上面的代码调用如下所示的控制器方法getMenuChildren:

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;
}

在表单上单击保存按钮时,将执行EditAction,如下所示:

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

感谢您的帮助。我怎样才能获得表格验证?再次,我得到在大海捞针中找不到输入

0 个答案:

没有答案