Skip to content
Permalink
Browse files

Removing Validation and isolating Form to Responder

  • Loading branch information...
andrewshell committed Sep 27, 2016
1 parent 6ca6a7b commit 2ed2da998c9084bff97c647d2cf158a9903b5d70
Showing with 18 additions and 49 deletions.
  1. +1 −12 src/Config.php
  2. +3 −15 src/RegistrationInput.php
  3. +8 −2 src/RegistrationResponder.php
  4. +2 −12 src/RegistrationType.php
  5. +4 −8 web/index.php
@@ -10,15 +10,13 @@
use Symfony\Bridge\Twig\Form\TwigRendererEngine;
use Symfony\Component\Form\Extension\Csrf\CsrfExtension;
use Symfony\Component\Form\Extension\Core\CoreExtension;
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
use Symfony\Component\Form\FormFactoryBuilder;
use Symfony\Component\Form\Forms;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Security\Csrf\CsrfTokenManager;
use Symfony\Component\Security\Csrf\TokenGenerator\UriSafeTokenGenerator;
use Symfony\Component\Security\Csrf\TokenStorage\SessionTokenStorage;
use Symfony\Component\Translation\Loader\XliffFileLoader;
use Symfony\Component\Validator\Validation;
use Twig_Environment;
use Twig_Extension_Debug;
use Twig_Loader_Filesystem;
@@ -39,12 +37,9 @@ public function define(Container $di)
$di->set('symfony/form:factory', $di->lazyGetCall('symfony/form:factory:builder', 'getFormFactory'));
$di->set('twig:environment', $di->lazyNew(Twig_Environment::class));
$di->params[RegistrationInput::class] = [
'formFactory' => $di->lazyGet('symfony/form:factory'),
];
$di->params[RegistrationResponder::class] = [
'twig' => $di->lazyGet('twig:environment'),
'formFactory' => $di->lazyGet('symfony/form:factory'),
];
/** SessionTokenStorage */
@@ -112,16 +107,10 @@ public function define(Container $di)
/** TranslationExtension */
$di->params[TranslationExtension::class]['translator'] = $di->lazyNew(Translator::class);
/** ValidatorExtension */
$di->params[ValidatorExtension::class] = [
'validator' => $di->lazy([Validation::class, 'createValidator']),
];
/** FormFactoryBuilder */
$di->setters[FormFactoryBuilder::class]['addExtensions'] = new LazyArray([
$di->lazyNew(CoreExtension::class),
$di->lazyNew(CsrfExtension::class),
$di->lazyNew(ValidatorExtension::class),
]);
}
@@ -1,24 +1,12 @@
<?php
namespace FutureProofPhp;
use Psr\Http\Message\ServerRequestInterface as PsrRequest;
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
use Symfony\Component\Form\FormFactory;
use Psr\Http\Message\ServerRequestInterface as Request;
class RegistrationInput
{
private $formFactory;
public function __construct($formFactory)
public function __invoke(Request $request)
{
$this->formFactory = $formFactory;
}
public function __invoke(PsrRequest $psrRequest)
{
$form = $this->formFactory->create(RegistrationType::class);
$form->handleRequest();
return [$form];
return [$request->getParsedBody()];
}
}
@@ -3,22 +3,28 @@
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Symfony\Component\Form\FormFactory;
use Twig_Environment;
class RegistrationResponder
{
private $twig;
private $formFactory;
public function __construct(Twig_Environment $twig)
public function __construct(Twig_Environment $twig, FormFactory $formFactory)
{
$this->twig = $twig;
$this->formFactory = $formFactory;
}
public function __invoke(
Request $request,
Response $response,
$form
array $data
) {
$form = $this->formFactory->create(RegistrationType::class);
$form->handleRequest();
$response->withHeader('Content-Type', 'text/html');
$response->getBody()->write($this->twig->render('index.html.twig', array(
'form' => $form->createView(),
@@ -15,18 +15,8 @@ class RegistrationType extends AbstractType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('firstName', TextType::class, array(
'constraints' => array(
new NotBlank(),
new Length(array('min' => 4)),
),
))
->add('lastName', TextType::class, array(
'constraints' => array(
new NotBlank(),
new Length(array('min' => 4)),
),
))
->add('firstName', TextType::class)
->add('lastName', TextType::class)
->add('gender', ChoiceType::class, array(
'choices' => array('Male' => 'male', 'Female' => 'female'),
'expanded' => true,
@@ -21,18 +21,14 @@
$adr->middle('Radar\Adr\Handler\ActionHandler');
/** Routes */
$adr->get('getHome', '/', function (Form $form) {
return $form;
$adr->get('getHome', '/', function (array $data) {
return $data;
})
->input('FutureProofPhp\RegistrationInput')
->responder('FutureProofPhp\RegistrationResponder');
$adr->post('postHome', '/', function (Form $form) {
if ($form->isValid()) {
// Do Something
$data = $form->getData();
}
return $form;
$adr->post('postHome', '/', function (array $data) {
return $data;
})
->input('FutureProofPhp\RegistrationInput')
->responder('FutureProofPhp\RegistrationResponder');

0 comments on commit 2ed2da9

Please sign in to comment.
You can’t perform that action at this time.