渲染控制器并从子级获取表单错误 [英] Render controller and get form errors from child

查看:27
本文介绍了渲染控制器并从子级获取表单错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模板,我在其中渲染一个包含表单的小部件:

I have a template where I render a widget which contains a form:

{{ render(controller('ApplicationDemoBundle:Demo:newWidget', {'demo' : entity })) }}

newWidgetAction 调用 createAction:

The newWidgetAction calls a createAction:

public function createAction(Request $request)
{
    $entity = new Demo();
    $form = $this->createCreateForm($entity);
    $form->handleRequest($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($entity);
        $em->flush();

        return $this->redirect($this->generateUrl('demo_show', array('id' => $entity->getId())));

    }

    return array(
        'entity' => $entity,
        'form'   => $form->createView(),
    )
    // Something like this would be awesome with passing the form containing errors with
    // return $this->redirect($this->getRequest()->headers->get('referer'));
}

想象一下提交的表单(用户在表演主题中的行为)产生了一个错误.这将返回到不显示完整布局的 newWidget 模板.

Imagine the submitted form (user acts in show theme) produces an error. This would make a return to the newWidget template which does not display the full layout.

我现在的问题是:在不修改 showActions 函数参数的情况下,将错误从子控制器 (newWidget) 传递到主模板 (show) 的正确方法是什么?

My question is now: What is the right way to pass the errors from the child controller (newWidget) to the main template (show)?, without modifying the showActions function parameter to pass the formerrors over there.

这个问题有一个类似的线索:Symfony2 渲染和表单在这种使用会话的情况下,但我非常好奇这是不是要走的路.

There is a similar thread to this question: Symfony2 render and form In this case where sessions used but I'm more than curious if this is the way to go.

推荐答案

问题是每个片段(一个子控制器)使用一个虚拟请求.这是为了保护原始请求免受可能意外转发的修改,而片段本质上是在渲染阶段发生的转发.

The problem is that each fragment (a sub-controller) uses a virtual request. This is to protect the original request from modification by possibly unexpected forwards, and a fragment is essentially a forward taking place during a rendering stage.

可以使用以下方法访问顶级请求:$this->container->get('request'); 然后用片段中的表单处理请求,但是如果每页使用多个表单,这可能会很快变得非常混乱.

It is possible to access the top level request using: $this->container->get('request'); then handing the request with the form in the fragment, but this may get very confusing very quickly if you are using multiple forms per page.

我的策略是遵循一个约定,将页面上验证表单的数量限制为一个.任何其他表单都不需要验证,否则表单不可能被错误地提交(被黑的表单会抛出服务器端异常,但用户应该只在它们顽皮时才能看到这些异常).

My strategy is to follow a convention that limits the number of validated form on a page to just one. Any other forms don't require validation, or it is otherwise impossible for a form to be submitted incorrectly (hacked forms would throw server side exceptions, but the user should only see those if they are being naughty).

尝试构建模板继承结构以适应表单导航,同时始终显示大部分相同的布局和数据.您可以通过扩展片段的使用来实现这一点,这会带来分离显示逻辑的好处.

Try to structure your template inheritance to accommodate navigation to forms, while always showing most of the same layouts and data. You can do this by expanding the use of fragments which gives the bonus of separating your display logic.

这篇关于渲染控制器并从子级获取表单错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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