Zend framework2 jquery验证不需要';不适用于ajax加载的引导模式对话框内容

Zend framework2 jquery验证不需要';不适用于ajax加载的引导模式对话框内容,zend-framework2,jquery-validate,twitter-bootstrap-3,bootstrap-modal,Zend Framework2,Jquery Validate,Twitter Bootstrap 3,Bootstrap Modal,我正在使用zend 2的引导。 模态的内容通过ajax调用检索。 但验证根本不起作用,它提交时未检查任何字段: 以下是模态载荷的位置: <div class="modal fade" id="themesModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div cla

我正在使用zend 2的引导。 模态的内容通过ajax调用检索。 但验证根本不起作用,它提交时未检查任何字段:

以下是模态载荷的位置:

<div class="modal fade" id="themesModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">

        </div>
    </div>
</div>
以下是表格代码:

 $this->add(array(
            'name' => 'name',

            'attributes' => array(
                'type'  => 'text',
                'id' => 'username',
                'class'  => 'form-control',
                'required'  => 'true',
                'minlength'  => '8',
                'maxlength'  => '20',
            ),
            'options' => array(
                'label' => 'name',
            ),
        ));

        $this->add(array(
            'name' => 'description',

            'attributes' => array(
                'type'  => 'password',
                'id' => 'password',
                'class'  => 'form-control',
                'required'  => 'true',
                'minlength'  => '8',
                'maxlength'  => '20',
            ),
            'options' => array(
                'label' => 'Password',
            ),
        ));

我用一种不同的方法来处理这个问题

您应该通过AJAX提交表单,让ZF2返回带有表单标记的
JsonModel
,而不是尝试在客户端进行自己的验证(添加错误HTML等)

如果您使用的是正确的视图帮助程序(
FormRow
),它将正确呈现表单错误

比如说

class FooController extends AbstractActionController
{

   public function createAction()
   {
     $request = $this->getRequest();
     $service = $this->getServiceLocator()->get('FooService'); 
     $form    = $service->getFooCreateForm(); // Zend\Form

     if ($request->isPost()) {
        $form->setData($request->getPost());

        if ($form->isValid()) {

            $foo = $service->create($form->getData());

            if ($foo instanceof Foo)
                // redirect to success page
            } else {
                $this->flashMessenger()->addErrorMessage('Error!');
            }
            return $this->redirect()->toRoute('foo', array('action' => 'index'));

        } else if ($request->isXmlHttpRequest()) {

            $renderer = $this->getServiceLocator()->get('viewrenderer');
            $formView = new ViewModel(compact('form')); // add the form to the view
            $formView->setTemplate('foo-module/foo/form');

            return new JsonModel(array(
                'success' => false, // Flag to keep modal open
                'content' => $renderer->render($formView), // The "content" being the form HTML
                'message' => 'Please check all form fields have been completed successfully',
            ));
        }
     }
     return new ViewModel(compact('form'));
   }

}
form.phtml

<?php
  echo $this->form($this->form);
//...modal html
<div class="modal-body">
    <?php echo $this->partial('form.phtml', compact('form')); ?>
</div>
//... modal html

//... 模态html
你还想:

  • 从ZF2表单中删除所有按钮(因为modal已经有两个按钮)
  • 将JS事件监听器添加到模式提交按钮,AJAX发布表单内容 到
    createAction
  • 如果调用的结果是
    success==true
    ,则关闭模式
  • 如果结果为
    success==false
    您将拥有
    content
    变量,该变量可以使用
    $('.modal content').html(result.content)
    替换现有的模式内容,这将包括已验证的表单html(包括表单视图帮助程序生成的所有错误)

事实上,所有的服务器端验证机制都已就位,并且运行良好

我需要通过jquery进行客户端验证

由于<强> Bootstrap 模式对话框是从<强> Ajax < /St>中加载的,它没有像平常那样考虑周围的上下文,我需要在Ajax内容中插入<强> jQuery 代码。 ZF2不允许在其视图文件中插入js代码

因此,我以以下方式在ajax内容中插入了以下行:

 <?php echo $this->inlineScript()->appendFile($this->basePath() . '/js/custom.js')?>


不显示Zend代码,而是显示浏览器看到的相关HTML标记。@Sparky我可以用包含上述HTML代码的js脚本显示相关HTML“证明”该标记适用于JavaScript。我们还应该如何确定jQuery验证脚本为什么不工作?@Sparky请看我的回答虽然始终进行服务器端验证是个好主意,但在这种情况下,OP询问的是客户端验证。通常两者都使用;客户端用于获得更好的用户体验,服务器端用于防止前者失败。@Sparky您所指的“更好的体验”是为了避免页面刷新,对吗?这种方法通过AJAX提交表单,并使用表单视图助手构建HTML;无页面重新加载;您需要做的就是用AJAX调用的响应替换表单内容。在我看来,这比复制可能复杂的验证服务器端(您必须通过javascript添加HTML内容)要好。这将很难维护和扩展-例如,如果您要在表单中添加一个字段。我的主要观点是,您的解决方案没有专门解决OP的问题。
//...modal html
<div class="modal-body">
    <?php echo $this->partial('form.phtml', compact('form')); ?>
</div>
//... modal html
 <?php echo $this->inlineScript()->appendFile($this->basePath() . '/js/custom.js')?>