Skip to content

Instantly share code, notes, and snippets.

@masarliev
Created January 22, 2014 23:34
Show Gist options
  • Select an option

  • Save masarliev/8569778 to your computer and use it in GitHub Desktop.

Select an option

Save masarliev/8569778 to your computer and use it in GitHub Desktop.
ZF2 form from doctrine entity & annotations
<?php
namespace Application\Entity;
use Doctrine\ORM\Mapping as ORM;
use Zend\Form\Annotation;
/**
* Class Account
* @package Application\Entity
* @ORM\MappedSuperclass
*/
class Account
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
* @Annotation\Exclude
*/
protected $id;
/**
* @ORM\Column(type="string")
* @Annotation\Type("Zend\Form\Element\Text")
* @Annotation\Required({"required":"true" })
* @Annotation\Filter({"name":"StripTags"})
* @Annotation\Validator({"name":"StringLength", "options":{"min":"1"}})
* @Annotation\Options({"label":"Username:"})
*/
protected $username;
/**
* @ORM\Column(type="string")
* @Annotation\Type("Zend\Form\Element\Password")
* @Annotation\Required({"required":"true" })
* @Annotation\Validator({"name":"StringLength", "options":{"min":"6"}})
* @Annotation\Options({"label":"Password:"})
*/
protected $password;
/**
* @Annotation\Type("Zend\Form\Element\Submit")
* @Annotation\Attributes({"value":"Submit"})
*/
protected $submit;
/**
* @param mixed $password
*/
public function setPassword($password)
{
$this->password = md5($password);
}
/**
* @param mixed $username
*/
public function setUsername($username)
{
$this->username = $username;
}
/**
* @return mixed
*/
public function getPassword()
{
return $this->password;
}
/**
* @return mixed
*/
public function getUsername()
{
return $this->username;
}
}
<?php
namespace Application\Controller;
use Application\Entity\User;
use DoctrineORMModule\Form\Annotation\AnnotationBuilder;
use Zend\Mvc\Controller\AbstractActionController;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
use Zend\View\Model\ViewModel;
/**
* Class IndexController
* @package Application\Controller
*/
class IndexController extends AbstractActionController
{
public function indexAction()
{
/** @var $em \Doctrine\ORM\EntityManager */
$em = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
if (($user = $em->find('Application\Entity\User', 1)) === null) {
$user = new User();
}
$builder = new AnnotationBuilder($em);
$form = $builder->createForm($user);
$form->setHydrator(new DoctrineHydrator($em, 'Application\Entity\User'))->bind($user);
$request = $this->getRequest();
if ($request->isPost()) {
$form->setData($request->getPost());
if ($form->isValid()) {
$em->persist($user);
$em->flush();
return $this->redirect()->toRoute('home');
}
}
return new ViewModel(array('form'=>$form));
}
}
<?php
namespace Application\Entity;
use Doctrine\ORM\Mapping as ORM;
use Zend\Form\Annotation;
/**
* Class User
* @package Application\Entity
* @ORM\Entity
* @ORM\Table(name="users")
* @Annotation\Name("user")
*/
class User extends Account
{
/**
* @ORM\Column(type="string")
* @Annotation\Type("Zend\Form\Element\Email")
* @Annotation\Required({"required":"true" })
* @Annotation\Filter({"name":"StripTags"})
* @Annotation\Validator({"name":"StringLength", "options":{"min":"1"}})
* @Annotation\Validator({"name":"EmailAddress"})
* @Annotation\Options({"label":"E-Mail:"})
*/
protected $email;
public function setEmail($param)
{
$this->email = $param;
}
/**
* @return mixed
*/
public function getEmail()
{
return $this->email;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment