Created
January 22, 2014 09:32
-
-
Save francisbesset/8555929 to your computer and use it in GitHub Desktop.
Symfony2: Form CheckboxType management with Domain object
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 Acme\Bundle\DemoBundle\Form\Type; | |
use Acme\Bundle\DemoBundle\Domain\DemoDomain; | |
use Acme\Bundle\DemoBundle\Entity\Demo; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\OptionsResolver\OptionsResolverInterface; | |
class DemoType extends AbstractType | |
{ | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
$builder | |
->add('name', 'text', array( | |
'property_path' => 'demo.name', | |
)) | |
->add('regenerateApiKey', 'checkbox', array( | |
'required' => false, | |
)) | |
->add('save', 'submit'); | |
} | |
public function setDefaultOptions(OptionsResolverInterface $resolver) | |
{ | |
$resolver->setDefaults(array( | |
'data_class' => 'Acme\Bundle\DemoBundle\Domain\DemoDomain', | |
'empty_data' => new DemoDomain(new Demo()), | |
)); | |
} | |
public function getName() | |
{ | |
return 'acme_demo'; | |
} | |
} |
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 Acme\Bundle\DemoBundle\Entity; | |
use Doctrine\ORM\Mapping as ORM; | |
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; | |
use Symfony\Component\Validator\Constraints as Assert; | |
/** | |
* @ORM\Entity | |
* @ORM\Table(name="demo") | |
* @UniqueEntity(fields={"name"}, errorPath="name", message="The name already exists.") | |
*/ | |
class Demo | |
{ | |
/** | |
* @ORM\Id | |
* @ORM\Column(type="integer") | |
* @ORM\GeneratedValue(strategy="AUTO") | |
*/ | |
protected $id; | |
/** | |
* @ORM\Column(type="string", length=20, unique=true) | |
* @Assert\NotBlank | |
* @Assert\Length(max=20) | |
*/ | |
protected $name; | |
/** | |
* @ORM\Column(name="api_key", type="string", length=50, nullable=true) | |
* @Assert\Length(max=50) | |
*/ | |
protected $apiKey; | |
public function getId() | |
{ | |
return $this->id; | |
} | |
public function getName() | |
{ | |
return $this->name; | |
} | |
public function setName($name) | |
{ | |
$this->name = $name; | |
} | |
public function getApiKey() | |
{ | |
return $this->apiKey; | |
} | |
public function setApiKey($apiKey) | |
{ | |
$this->apiKey = $apiKey; | |
} | |
public function regenerateApiKey() | |
{ | |
$this->setApiKey(base_convert(sha1(uniqid(mt_rand(), true)), 16, 36)); | |
} | |
} |
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 Acme\Bundle\DemoBundle\Controller; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
use Symfony\Component\HttpFoundation\Request; | |
class DemoController extends Controller | |
{ | |
public function createAction(Request $request) | |
{ | |
$form = $this->createForm('acme_demo'); | |
if ($request->isMethod('POST')) { | |
$form->bind($request); | |
if ($form->isValid()) { | |
$demoDomain = $form->getData(); | |
$demoDomain->save(); | |
$demo = $demoDomain->getDemo(); | |
$em = $this->getEntityManager(); | |
$em->persist($demo); | |
$em->flush(); | |
return $this->redirect($this->generateUrl('acme_demo_my_route', array('id' => $demo->getId()))); | |
} | |
} | |
return $this->render('AcmeDemoBundle:Demo:edit.html.twig', array( | |
'form' => $form->createView(), | |
)); | |
} | |
protected function getRepository() | |
{ | |
return $this->getEntityManager()->getRepository('AcmeDemoBundle:Demo'); | |
} | |
protected function getEntityManager() | |
{ | |
return $this->container->get('doctrine')->getManagerForClass('AcmeDemoBundle:Demo'); | |
} | |
} |
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 Acme\Bundle\DemoBundle\Domain; | |
use Acme\Bundle\DemoBundle\Entity\Demo; | |
use Symfony\Component\Validator\Constraints as Assert; | |
class DemoDomain | |
{ | |
/** | |
* @Assert\Type("bool") | |
*/ | |
public $regenerateApiKey; | |
/** | |
* @Assert\Valid | |
*/ | |
protected $demo; | |
public function __construct(Demo $demo) | |
{ | |
$this->demo = $demo; | |
$this->regenerateApiKey = false; | |
} | |
public function getDemo() | |
{ | |
return $this->demo; | |
} | |
public function save() | |
{ | |
if ($this->regenerateApiKey) { | |
$this->demo->regenerateApiKey(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment