Symfony2 createFormBuilder和Flashbag问题

时间:2014-06-10 13:01:30

标签: php forms symfony flash-message

我使用单个操作来显示和提交使用createFormBuilder构建的表单

public function fooBar(){

    // ... creating form and stuff


    // on first Request i want to use the FlashBag to hold a "Fill out !" message

    // how to if here ? if(first request){
       $this->get('session')->getFlashBag()->add('notice', "Fill out!");
    // }
    if ($form->isValid()) {
        $fooService->create($bar);
        $this->get('session')->getFlashBag()->clear('notice');
        $this->get('session')->getFlashBag()->add('notice', "Succesfully Submitted !");
    } else {
        $this->get('session')->getFlashBag()->add('notice', "Form is Invalid!");
    }


    // otherwise i see "Fill Out!" and "Form is Invalid" on the First request when i did not fill out anything yet

}

我希望我的问题是可以理解的,

本质上,如果没有提交表单,则isValid()的计算结果为false,如果提交的表单包含无效的属性,则计算结果为false

我希望在第一次显示表单时显示特定的闪光消息

1 个答案:

答案 0 :(得分:1)

您可以为此目的检查Request方法。

// ... creating form and stuff
$request = $this->get('request');

// on first Request i want to use the FlashBag to hold a "Fill out !" message
if ($request->isMethod('GET')) 
{
    $this->get('session')->getFlashBag()->add('notice', "Fill out!");
}