Skip to content

Instantly share code, notes, and snippets.

@j
Created May 28, 2011 04:35
Show Gist options
  • Save j/996601 to your computer and use it in GitHub Desktop.
Save j/996601 to your computer and use it in GitHub Desktop.
services:
form.handler:
class: JStout\MainBundle\Component\Form\FormHandler
scope: request
arguments: [@request, @doctrine.orm.entity_manager]
<?php
namespace JStout\MainBundle\Component\Form;
use Symfony\Component\Form\Form,
Doctrine\ORM\EntityManager,
Symfony\Component\HttpFoundation\Request,
JStout\MainBundle\Component\Form\FormHandlerInterface;
class FormHandler
{
protected $request;
protected $entityManager;
public function __construct(Request $request, EntityManager $entityManager)
{
$this->request = $request;
$this->entityManager = $entityManager;
}
public function create(FormHandlerInterface $handler, Form $form)
{
$handler->buildFormHandler($form, $this->request, $this->entityManager);
return $handler;
}
}
<?php
namespace JStout\MainBundle\Component\Form;
use Symfony\Component\Form\Form,
Symfony\Component\HttpFoundation\Request,
Doctrine\ORM\EntityManager;
interface FormHandlerInterface
{
public function buildFormHandler(Form $form, Request $request, EntityManager $entityManager);
}
<?php
namespace JStout\MainBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller,
Sensio\Bundle\FrameworkExtraBundle\Configuration\Route,
Sensio\Bundle\FrameworkExtraBundle\Configuration\Template,
JStout\MainBundle\Entity\User,
JStout\MainBundle\Form\User\SignupType,
JStout\MainBundle\Form\User\SignupHandler;
/**
* @Route("/user")
*/
class UserController extends Controller
{
/**
* @Route(name="user")
* @Template()
*/
public function indexAction()
{
return array();
}
/**
* @Route("/sign-up", name="user_signup")
* @Template()
*/
public function signupAction()
{
$user = new User();
$form = $this->get('form.factory')->create(new SignupType(), $user);
$formHandler = $this->get('form.handler')->create(new SignupHandler(), $form);
if ($formHandler->process($user, $this->container->get('security.encoder_factory')->getEncoder($user))) {
return $this->redirect($this->generateUrl('success'));
}
return array(
'form' => $form->createView()
);
}
}
<?php
namespace JStout\MainBundle\Form\User;
use JStout\MainBundle\Component\Form\FormHandlerInterface,
Symfony\Component\Form\Form,
Symfony\Component\HttpFoundation\Request,
Doctrine\ORM\EntityManager,
JStout\MainBundle\Entity\User;
class SignupHandler implements FormHandlerInterface
{
protected $form;
protected $request;
protected $entityManager;
public function buildFormHandler(Form $form, Request $request, EntityManager $entityManager)
{
$this->form = $form;
$this->request = $request;
$this->entityManager = $entityManager;
}
public function process(User $user, $encoder)
{
if ('POST' == $this->request->getMethod()) {
$this->form->bindRequest($this->request);
if ($this->form->isValid()) {
$user->setSalt(base_convert(sha1(uniqid(mt_rand(), true)), 16, 36));
$user->setPassword($encoder->encodePassword($user->getPlainPassword(), $$user->getSalt()));
$this->entityManager->persist($user);
$this->entityManager->flush();
return true;
}
}
return false;
}
}
<?php
namespace JStout\MainBundle\Form\User;
use Symfony\Component\Form\AbstractType,
Symfony\Component\Form\FormBuilder;
class SignupType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('firstName')
->add('lastName')
->add('email')
->add('plainPassword', 'repeated', array('type' => 'password'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment