john Smith
john Smith

Reputation: 17906

Symfony2 createFormBuilder and Flashbag issue

i´m using a single action to Display and submit a form built with 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

}

i hope my problem is understandable,

essentially its that isValid() evaluates to false if no form was submitted and also evaluates to false if the submitted form contained invalid propertys

and i want to display a specific flasmessage on the first time the form is displayed

Upvotes: 0

Views: 159

Answers (1)

Debreczeni András
Debreczeni András

Reputation: 1678

You can check the Request method for this purpose.

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

Upvotes: 1

Related Questions