0

我正在使用单个操作来显示和提交使用 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

我想在第一次显示表单时显示一个特定的 flasmessage

4

1 回答 1

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!");
}
于 2014-06-10T13:06:01.497 回答