簡體   English   中英

Symfony2 createFormBuilder和Flashbag問題

[英]Symfony2 createFormBuilder and Flashbag issue

我正在使用一個動作來顯示和提交使用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

我想在第一次顯示表單時顯示特定的裝飾

您可以為此目的檢查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!");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM