javascript - Zend 1.12 + Ajax - 提交表单

标签 javascript jquery forms zend-framework twitter-bootstrap-3

用户单击提交表单后,我需要调用该表单的 Zend 验证而不刷新整个页面。我还在我的网站中使用 zend_Layout 。我在这里看了很多教程,但仍然无法使其工作。

索引 Controller :

class IndexController extends Zend_Controller_Action {

    public function init() {

    }

    public function indexAction() {
$this->view->static_data = "eg. ABCDEFG"; 

        $this->view->form = new Application_Form_Test();
    }
    public function ajaxAction() {
        // probably some code to hande ajax 
    }
}

查看索引/索引:

...
<?php 
echo date('m/d/Y h:i:s a', time());
echo $this->static_data; 
?>

<hr />
<?php echo $this->form ?>
...

表格:

class Application_Form_Test extends Zend_Form
{
    public function init()
    {
        $this->setMethod('post');
        $this->setAttrib('class', 'form1');

        $this->addElement('text', 'email', array(
            'label'      => 'Your email address:',
            'required'   => true,
            'filters'    => array('StringTrim'),
            'validators' => array(
                'EmailAddress',
            )
        ));

        $this->addElement('text', 'name', array(
            'label'      => 'Your name:',
            'required'   => true,
            'validators' => array(
                array('validator' => 'StringLength', 'options' => array(3, 20))
                )
        ));

        // Add the submit button
        $this->addElement('submit', 'submit', array(
            'ignore'   => true,
            'label'    => 'Send',
        ));

        // And finally add some CSRF protection
        $this->addElement('hash', 'csrf', array(
            'ignore' => true,
        ));
    }
}

那么我如何在不刷新该页面其余部分的情况下验证表单,并在表单无效时查看 Zend 错误消息。

最佳答案

您可以将表单发布到 Ajax 操作,您将在其中实例化表单并从请求注入(inject)数据。

$form = new Form();
if ($this->getRequest()->isPost()) {
    if ($form->isValid($this->getRequest()->getPost())) {
        //save data
        ....
    }
}
$this->view->form = $form;

您有两个选择:

  • 在 View 中呈现表单并使用 HTML 进行响应。使用 JavaScript 将当前表单替换为 Ajax 请求返回的 HTML。
  • 使用 Zend_Form::getMessages() 获取错误消息并使用 JSON 进行响应。

    $this-view->messages = $form->getMessages();

关于javascript - Zend 1.12 + Ajax - 提交表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28605452/

相关文章:

javascript - 包含数据库中相关信息的打开模式

javascript - Meteor 中的 collection.insert 问题?

javascript - 每 4 个元素重复列表中的类

javascript - 根据复选框获取输入值的脚本

php - href 超链接为 POST

javascript - 共享http库

javascript 数组对象转换一个键,数组值单个对象

javascript - 将列的值从一个表传输到另一个表

javascript - 禁用提交按钮而不产生灰显效果

javascript - 如何区分javascript提交和手动点击提交