Php Symfony2 createFormBuilder和Flashbag问题

Php Symfony2 createFormBuilder和Flashbag问题,php,forms,symfony,flash-message,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('

我使用单个操作来显示和提交使用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;如果提交的表单包含无效属性,isValid()也将计算为false


我想在第一次显示表单时显示特定的flas消息

为此,您可以检查
请求
方法

// ... 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!");
}

是的,这很有效,但我想我找到了更好的->如果($form->isSubmitted()){这适用于你的上一个问题,但你的第一个问题是“在第一次请求时,我想使用FlashBag”。在这种情况下,最好的确定方法是通过请求方法检查。你是对的,它完全匹配!我不知道,symfony表单总是使用POSTYes,如果省略form
method
,则
POST
设置为def故障输入
Symfony