samedi 28 février 2015

Symfony 2: How to build a really simple form, with just one field and use it in controller

I'm building a get started procedure. It is really very simple: just one field for the email and a submit button.


HOW DOES THE PROCEDURE WORKS I simply have one controller with two methods: indexAction() and endAction() The indexAction simply set the route using annotations and displays the twig template with an handmade form:



namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;

class GetStartedController extends Controller
{
/**
* @Route("getstarted")
* @Template()
*/
public function indexAction()
{
return array(
// ...
);
}

/**
* @Route("getstarted/end", name="getStartedEnd")
* @Template()
*/
public function endAction(Request $request)
{

// ... other code


As you can see, currently the method really does nothing as the form is handmade directly in the twig template.


Here is the code to render the form (handmade in the twig template):



{% extends "::base.html.twig" %}

{% block title %}AppBundle:GetStarted:index{% endblock %}

{% block body %}
<form id="getStarted" action="{{ path('getStartedEnd') }}" method="post">
<div class="form-group">
<input type="email" name="userEmail" class="form-control input-lg" placeholder="Enter your e-mail (es.: your.name@example.com">
</div>
<button type="submit" name="submit" class="btn btn-default btn-lg">Submit</button>
</form>
{% endblock %}


I know Symfony 2 can handle the creation of the form and its rendering on the page, but this is a very simple form and I know it never will be more complicate, so an handmade one is the simplest and fastest solution.


Now, what's the problem? The problem is I don't understand how to get the form and its submitted values in my controller, in the other method, endAction().


The form has as action the path to the other method, endAction() where, in my intentions, I will retrieve the submitted values and do some stuff with them.


Following the instructions in paragraph Using a Form without a Class, i came up with the following code for the method endAction() that, in my intentions, will retrieve the submitted email and do something with it - will create a user, obviously - (note the exit to simply print the results of the if)



/**
* @Route("getstarted/end", name="getStartedEnd")
* @Template()
*/
public function endAction(Request $request)
{
$form = $this->createFormBuilder()
->add('userEmail', 'email')
->add('submit', 'submit')
->getForm();

$form->handleRequest($request);

if ($form->isValid()) {
$data = $form->getData();
print_r($data);
} else {
echo 'no data submitted';
}

exit;


return array(
// ...
);
}


The problem is that if ($form->isValid()) { ever returns false and I ever obtain as result the printing of echo 'no data submitted';


Clearly there is something wrong with my implementation of the form handling, but... What?


This is my question: what am I doing wrong? Why the form isn't "intercepted" in the controller?


Aucun commentaire:

Enregistrer un commentaire