02/2015 / Milan "perún" Herda / @moriquend / hrad.perunhq.org
{
"require": {
"symfony/form": "~2.6.3"
}
}
// symfony controller
$formBuilder = $this->get('form.factory')->createBuilder()
// $formBuilder = $this->createFormBuilder();
// silex controller
$formBuilder = $app['form.factory']->createBuilder()
$formBuilder
->add('name')
->add('email')
->add('gender', 'choice', [
'choices' => [
'male' => 1,
'female' => 2,
]
])
->add('submit', 'submit');
$form = $formBuilder->getForm();
// zistenie odoslania a naplnenie dátami z requestu
$form->handleRequest($request);
// formulár bol odoslaný a je validný
if ($form->isValid()) {
$data = $form->getData();
// spracovať dáta
// nastaviť flash message
// urobiť redirect
}
$defaultData = [
'name' => 'Ferko',
'email' => 'ferko.mrkvicka@example.com',
'gender' => 1,
];
// symfony controller
$formBuilder = $this->get('form.factory')->createBuilder('form', $defaultData)
// $formBuilder = $this->createFormBuilder($defaultData);
// silex controller
$formBuilder = $app['form.factory']->createBuilder('form', $defaultData)
use Symfony\Component\Validator\Constraints as Assert;
// pre silex potrebujeme aj zaregistrovať validátor
$app->register(new Silex\Provider\ValidatorServiceProvider());
$formBuilder
->add('name', 'text', [
'constraints' => [
new Assert\NotBlank(),
new Assert\Length(['min' => 3]),
]
])
->add('email', 'text', [
'constraints' => [
new Assert\NotBlank(),
new Assert\Email(),
]
])
->add('gender', 'choice', [
'choices' => [
'male' => 1,
'female' => 2,
],
'constraints' => [
new Assert\Choice([1, 2]),
]
])
->add('submit', 'submit');
Všetok tento kód sa teraz nachádza v controlleri
use Symfony\Component\Validator\Constraints as Assert;
$defaultData = [
'name' => 'Ferko',
'email' => 'ferko.mrkvicka@example.com',
'gender' => 1,
];
// symfony controller
$formBuilder = $this->get('form.factory')->createBuilder('form', $defaultData)
// silex controller
$app->register(new Silex\Provider\ValidatorServiceProvider());
$formBuilder = $app['form.factory']->createBuilder('form', $defaultData)
$formBuilder
->add('name', 'text', [
'constraints' => [
new Assert\NotBlank(),
new Assert\Length(['min' => 3]),
]
])
->add('email', 'text', [
'constraints' => [
new Assert\NotBlank(),
new Assert\Email(),
]
])
->add('gender', 'choice', [
'choices' => [
'male' => 1,
'female' => 2,
],
'constraints' => [
new Assert\Choice([1, 2]),
]
])
->add('submit', 'submit');
$form = $formBuilder->getForm();
$form->handleRequest($request);
if ($form->isValid()) {
$data = $form->getData();
// spracovať dáta
// nastaviť flash message
// urobiť redirect
}
namespace Profesia\UserBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Validator\Constraints as Assert;
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text', [
'constraints' => [
new Assert\NotBlank(),
new Assert\Length(['min' => 3]),
]
])
->add('email', 'text', [
'constraints' => [
new Assert\NotBlank(),
new Assert\Email(),
]
])
->add('gender', 'choice', [
'choices' => [
'male' => 1,
'female' => 2,
],
'constraints' => [
new Assert\Choice([1, 2]),
]
]);
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults([
]);
}
public function getName()
{
return 'UserType';
}
}
use Profesia\UserBundle\Form\Type\UserType;
// symfony controller
$form = $this->get('form.factory')->create(new UserType(), $defaultData)
// silex controller
$form = $app['form.factory']->create(new UserType(), $defaultData)
$form->handleRequest($request);
if ($form->isValid()) {
$data = $form->getData();
// spracovať dáta
// nastaviť flash message
// urobiť redirect
}
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
class UserHandler
{
public function handle(FormInterface $form, Request $request)
{
$form->handleRequest($request);
if (!$form->isSubmitted()) {
return false;
}
if (!$form->isValid()) {
return false;
}
$data = $form->getData();
// uložíme do db
// odpálime prípadné eventy cez event dispatcher
return true;
}
}
Pokiaľ má FormHandler nejaké závislosti (db, repozitár, event dispatcher, domain manager) ,tak ich odovzdáme cez jeho konštruktor (dependency injection).
Nie je dobré konštruovať formHandler priamo v controlleri, pretože jeho závislosti sa môžu meniť a zťažuje to možnosť testovať controller.
Takže si na FormHandler buď vytvoríme továrničku (factory) alebo ho zaregistrujeme do DI kontajneru.
$app['form_handler.user'] = $app->share(function ($app) {
return new UserHandler(
//prípadné závislosti sa injectujú ako parametre, napr.:
/*
$app['doctrine.orm.entity_manager'],
$app['event.dispatcher']
*/
);
});
use Profesia\UserBundle\Form\Type\UserType;
$form = $this->get('form.factory')->create(new UserType(), $defaultData)
$formHandler = $this->get('form_handler.user');
if ($formHandler->handle($form, $request)) {
// nastaviť flash message
// urobiť redirect
}
use Profesia\UserModule\Form\Type\UserType;
$form = $app['form.factory']->create(new UserType(), $defaultData)
$formHandler = $app['form_handler.user'];
if ($formHandler->handle($form, $request)) {
// nastaviť flash message
// urobiť redirect
}