Last Updated: February 25, 2016
·
4.167K
· klj613

Use form handlers in symfony2

Avoid polluting your controllers with form handling logic by creating a form handler like FooBundle/Form/Handler/RegisterHandler.php.

Then in your controller:

if ($handler->process($form)) {
    // success
} else {
    // fail
}

or

$foo = $handler->process($form);
if ($foo === true) {
    // success
} else {
    // fail - $foo contains something useful (e.g. why it failed)
}

or, set up the service to return a new object each time it is used and:

if ($handler->process($form)) {
    // success
} else {
    // fail - $handler->getBar(); contains something useful (e.g. why it failed)
}