Last active
November 28, 2015 20:16
-
-
Save jeremyb/0245f8f4749abf431864 to your computer and use it in GitHub Desktop.
Username value object mapped to a form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Form; | |
use AppBundle\Model\Username; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\DataMapperInterface; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\Form\FormError; | |
class UsernameType extends AbstractType implements DataMapperInterface | |
{ | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
$builder | |
->add('username', 'text') | |
->setDataMapper($this); | |
} | |
public function mapDataToForms($data, $forms) | |
{ | |
foreach ($forms as $form) { | |
if ('username' !== $form->getName() || null === $data) { | |
continue; | |
} | |
$form->setData($data->getUsername()); | |
} | |
} | |
public function mapFormsToData($forms, &$data) | |
{ | |
$forms = iterator_to_array($forms); | |
try { | |
$data = new Username($forms['username']->getData()); | |
} catch (\InvalidArgumentException $e) { | |
$error = new FormError($e->getMessage()); | |
$forms['username']->addError($error); | |
} | |
} | |
public function getName() | |
{ | |
return 'username'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Model; | |
use Webmozart\Assert\Assert; | |
class Username | |
{ | |
const MIN_LENGTH = 5; | |
const FORMAT = '/^[a-zA-Z0-9_]+$/'; | |
/** | |
* @var string | |
*/ | |
private $username; | |
public function __construct($username) | |
{ | |
Assert::notEmpty($username, 'The username should not be blank.'); | |
Assert::minLength($username, self::MIN_LENGTH, 'The username should contains at least %2$s characters.'); | |
Assert::regex($username, self::FORMAT, 'The username is invalid.'); | |
$this->username = $username; | |
} | |
/** | |
* @return string | |
*/ | |
public function getUsername() | |
{ | |
return $this->username; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Controller; | |
use AppBundle\Form\UsernameType; | |
use AppBundle\Model\Username; | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
use Symfony\Component\HttpFoundation\Request; | |
class DefaultController extends Controller | |
{ | |
/** | |
* @Route("/", name="creation") | |
*/ | |
public function creationAction(Request $request) | |
{ | |
$form = $this->createForm(new UsernameType()); | |
$form->add('submit', 'submit'); | |
$form->handleRequest($request); | |
if ($form->isSubmitted() && $form->isValid()) { | |
dump($form->getData()); exit; | |
} | |
return $this->render('default/index.html.twig', [ | |
'form' => $form->createView(), | |
]); | |
} | |
/** | |
* @Route("/edition", name="edition") | |
*/ | |
public function editionAction(Request $request) | |
{ | |
$username = new Username('jbarthe'); | |
$form = $this->createForm(new UsernameType(), $username); | |
$form->add('submit', 'submit'); | |
$form->handleRequest($request); | |
if ($form->isSubmitted() && $form->isValid()) { | |
dump($form->getData()); exit; | |
} | |
return $this->render('default/index.html.twig', [ | |
'form' => $form->createView(), | |
]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment