Ajax表单提交的Symfony2的优美降级为没有JavaScript的用户 [英] Ajax form submission in symfony2 with graceful degradation for users without javascript

查看:99
本文介绍了Ajax表单提交的Symfony2的优美降级为没有JavaScript的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立涉及形式的Web应用程序。正在使用的框架是Symfony2的。我想拥有一切工作没有JavaScript的用户,然后逐步增强体验的人启用JavaScript。我打算使用jQuery作为我的JavaScript库。

I am building a web application that involves forms. The framework being used is symfony2. I would like to have everything working for users without javascript, and then progressively enhance the experience for people with javascript enabled. I am planning to use JQuery as my javascript library.

我想有形式的地方提交,并使用FlashMessage组件,如果用户没有JavaScript的显示信息并请求是不是一个Ajax请求。

I would like to have forms where it submits and displays a message using the FlashMessage component if the user does not have javascript and the request is not an ajax request.

有关用户使用JavaScript的支持,我想将这些消息返回给他们,如果提交成功。随后的JavaScript应显示该消息立即股利。

For users with javascript support, I would like to return the message to them if submission is successful. The javascript should then display that message in a div immediately.

与此同时,我需要处理的错误消息。对于没有JavaScript的用户,整个窗体将重新呈现在地方的错误消息。

At the same time, I need to deal with error messages. For users without javascript, the whole form will be rerendered with the error messages in place.

有关用户使用JavaScript,Ajax响应应该是包含输入组件的ID和错误信息的数组。 Javascript的应该然后插入到这些形式没有刷新。

For users with javascript, the ajax response should be an array containing the id of the input component and the error message. Javascript should then insert these into the form without a refresh.

综观Symfony2的书,我可以看到下面的意见,我再修改它来检查,​​如果该请求是一个Ajax请求:

Looking at the symfony2 book, I can see the following for submissions, I then modified it to check if the request is an ajax request:

public function newAction(Request $request)
{
    // just setup a fresh $task object (remove the dummy data)
    $task = new Task();

    $form = $this->createFormBuilder($task)
        ->add('task', 'text')
        ->add('dueDate', 'date')
        ->getForm();

    if ($request->getMethod() == 'POST') {
        $form->bindRequest($request);

        if ($form->isValid()) {
            // perform some action, such as saving the task to the database
            if ($this->request->isXmlHttpRequest(){
                   //return data ajax requires.
            } 
            return $this->redirect($this->generateUrl('task_success'));
        }
    }

    // ...
}

我觉得这种方法的问题是,是不是很优雅。我需要补充的是额外的检查每一个形式。更进一步,如果说要求通过Zend_AMF进来这个方法是行不通的。

The problem I find with this approach is that is isn't very elegant. I need add that extra check every single form. Further more, this method would not work if say the request comes in via Zend_AMF.

有没有更好的办法来做到这一点?

Are there any better ways to do this?

推荐答案

另一种选择是有一个比其他回应你的控制器返回的东西,并使用 kernel.view 事件要改变这种成你想要的。这将是更好的干因为逻辑封装在听者。你只需要决定你希望你的控制器返回了什么。

Another option is to have your controller return something other than a response and use the kernel.view event to transform that into what you want. This would be better DRY because the logic is encapsulated in the listener. You just need to decide what you want your controllers to return.

if ($form->isValid()) {
    return new Something($redirectUrl, $ajaxData);
}

而在你的监听器:

And in your listener:

public function onKernelView($event)
{
    $request = $event->getRequest();
    $result = $event->getControllerResult();

    if ($result instanceof Something) {
        if ($request->isXmlHttpRequest()) {
            $event->setResponse(new Response(json_encode($result->getAjaxData())));
        } else {
            $event->setResponse(new RedirectResponse($result->getRedirectUrl()));
        }
    }
}

这篇关于Ajax表单提交的Symfony2的优美降级为没有JavaScript的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆