Posted on 15-04-22 by Peter

Forms

Forms is something you'll always use at a site, either to communicate with your visitors, letting them comment etc or whenever you want to add new content to your site. And unless it's only a form for mailing you most likely also need a database to store i.e a comment or a blogpost. Between the database and the form or when retireving data from the database we need to have an object (a entity). We put these classes in a folder named Entity in your bundle (you need to create this) and they are just plain regular php classes and you need for each variable a set and get function. So in my case I've four variables that I need in a blogpost, a title, an ingrees, the post itself and a date. I'm assuming you know how otherwise you need to learn basic php and OOP before beginning with Symfony. Read about it at wikipedia about OOP and at php.net

So we have our class, a function in a controller for the path where we want our form to be rendered. Next step is to add a in parameter for our function, the Request object which makes it possible for us to tell if the form has been submitted. You also need to add a use statement for your above created class which should be something like your project name/your bundlename/Entity/classname.
So in our function we first of all need an instance of out object and if you want the fields of the form to have a default value you also use the appropriate set function to do so. I've done so for the date field. Next step is to use the createFormBuilder function to create a Formbuilder object. To this object we need to add our fields that the form should be build of. So for each variable on our object that we want a field on the form we use the add function who takes the variable name and type of field as parameter. If we want, which I assume we all do, we need to add a submit button to be able to send our form and last thing to do is to return a finished form. did you follow? Probably not so let's see how it looks codewise.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
use Symfony\Component\HttpFoundation\Request;
use project\bundlename\Entity\Blog;

public function postBlogAction(Request $request) {
    $blog = new Blog();
    $blog->setDate(new \Datetime("today"));

    $form = $this->createFormBuilder($blog)
        ->add("title", "text")
        ->add("ingress", "text")
        ->add("post", "textarea")
        ->add("date", "date")
        ->add("save", "submit", array('label' => 'save post'))
        ->getForm();
}
          

So lot of talks but as you can see the code is really easy to write but we need to render the form and to do that we create a view and let a template output the form. Outputting the form in the template needs three lines of helper functions all taking the form as inparameter. The first (form_start) and the last simply creates the beginning and the end (form_end) of a form. The form_widget creates our fields etc.

1 2 3 4 5 6 7 8 9
//add to the function
return $this->render('LetitrockBlogBundle:Default:admin.html.twig', array(
          'form' => $form->createView(),));

//in your template
{{ form_start(form)}}
{{ form_widget(form)}}
{{ form_end(form)}}
          

It's nice and easy to create a form but we also need to take care of a submission and that is why we have the Request object as inparameter. This going to sound a little bit backwards but before we render the form we need to check if the form has been sent which is in the Request object. We also need to validate the form, if the form hasn't been sent the validation will return false and hence we render the form or the validation returns true and we do something with the information sent (saving to database is one way) and render some nice message to the poster. So to the function we add this code

1 2 3 4 5
$form->handleRequest($request);

if ($form->isValid()) {
  //return a response.
}
          

This is basic stuff and there are lot of more stuff that we can do with forms, i.e error handligt etc.