Last Updated: February 25, 2016
·
4.394K
· 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)
}

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Related Tags
#php
#symfony2
Did you know?

PHP applications still send a browser full of JavaScript. Hardening the server does not inventory the payment, analytics, and plugin scripts that run after the HTML is delivered.

Security insights from Cside