Symfony 2-布局嵌入“无实体/类形式";验证无效 [英] Symfony 2 - Layout embed "no entity/class form" validation isn't working

查看:45
本文介绍了Symfony 2-布局嵌入“无实体/类形式";验证无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用symfony开发一个博客,而且我陷入了版式中嵌入的表格的困境.就我而言,是一个简单的搜索表单.

I'm developing a blog in symfony and i'm stuck with forms that are embed inside the layout. In my case a simple search form.

<div class="b-header-block m-search">
    {{ render(controller('YagoQuinoySimpleBlogBundle:Blog:searchArticles')) }}
</div>

要渲染表单,我正在使用布局树枝文件内的嵌入控制器.

To render the form i'm using an embed controller inside the layout twig file.

public function searchArticlesAction(Request $request)
{
    $form = $this->createForm(new SearchArticlesType());

    $form->handleRequest($request);
    if ($form->isValid()) {
        // Do stuff here
    }

    return $this->render('YagoQuinoySimpleBlogBundle:Blog:searchArticles.html.twig', array(
                'form' => $form->createView()
    ));
}

indexAction是用于检索表单数据并过滤文章列表的那个.

The indexAction is the one that retrieves the form data and filters a list of articles.

public function indexAction(Request $request)
{
    $form = $this->createForm(new SearchArticlesType());
    $form->handleRequest($request);

    if ($form->isValid()) {
        $data = $form->getData();
        $criteria = array(
            'title' => $data['search']
        );
    } else {
        $criteria = array();
    }

    $articles = $this->getDoctrine()->getRepository('YagoQuinoySimpleBlogBundle:Article')->findBy($criteria, array(
        'createDateTime' => 'DESC'
            ), 5);

    return $this->render('YagoQuinoySimpleBlogBundle:Blog:index.html.twig', array('articles' => $articles));
}

SearchArticlesType是一个表单类

SearchArticlesType is a form class

class SearchArticlesType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('search', 'text', array(
            'constraints' => new NotBlank()
        ))
                ->add('submit', 'submit', array(
                    'label' => 'Buscar'
        ));
    }

    public function getName()
    {
        return 'searchArticles';
    }
}

问题是在我提交此表格时出现的.indexAction发挥作用,验证表单并过滤文章,但是当嵌入式控制器尝试验证数据时(仅用于显示信息或其他)

The problem comes when i submit this form. The indexAction do his part, validating the form and filtering the articles but when the embed controller tries to validate data (just for displaying info or whatever)

$form->handleRequest($request);
if ($form->isValid()) {
    // Do stuff here
}

我觉得我缺少什么.

谢谢您的帮助!

推荐答案

当您调用 render(controller('your_route'))时,实际上是在发出一个子请求,这意味着参数bag被清空.因此表单不会处理"您的请求.

When you call render(controller('your_route')) you are actually making a sub request which means the parameters bags are emptied so your request isn't "handled" by the form.

如果您使用的是2.4+版本,则可以使用..

If you are using 2.4+ you could get the master request from the request stack using ..

/** @var \Symfony\Component\HttpFoundation\RequestStack $requestStack */
$requestStack = $this->get('request_stack');

$masterRequest = $requestStack->getMasterRequest();

然后您可以在呈现的控制器中处理该请求,而不是像当前(子)请求一样.

And then you could handle that request in your rendered controller as opposed to the current (sub) request like..

$form->handleRequest($masterRequest);

这篇关于Symfony 2-布局嵌入“无实体/类形式";验证无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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