Introduction
So, it’s been a while since I’ve posted but I’ve been hard at work the last few evenings on a few very cool features.
- I’ve written a Spiffy\Entity class that you can extend Doctrine 2 entities from to gain a few new features.
- Zend Validator and Zend Filter annotations for entities.
- You can now validate directly on the model (this is how it should be, IMO).
- I’ve written Spiffy\Form and Spiffy\Dojo\Form that integrates with Spiffy\Entity to do field type detection automatically somewhat similar to Symfony 2.
- The form is smart enough to read the validators/filters from Spiffy\Entity. No more adding them directly to each form element!
- You have the option to autoPersist Spiffy\Entity if the form validates.
- I’ve added three new form elements – Spiffy_Form_Element_Entity, Spiffy_Dojo_Form_Element_ComboBoxEntity, and Spiffy_Dojo_Form_Element_FilteringSelectEntity.
- These elements allow you to map ManyToOne relations directly into your form. They’re still a work in progress (need to add store data option to the Dojo elements).
Usage
Let’s get to the nitty gritty and build a complete registration form, controller, and entity. At the time of writing (7/15/2011) this post is accurate, however, the latest documentation is always available on my GitHub repository.
Setting up the Spiffy library
- by downloading or cloning it into your include_path.
- Add the namespace to application.ini: autoloaderNamespaces[] = Spiffy
- Add the Spiffy pluginPaths to application.ini: pluginPaths.Spiffy_Application_Resource = “Spiffy/Application/Resource”
- Add the Spiffy annotations to Doctrine in your bootstrap: \Doctrine\Common\Annotations\AnnotationRegistr::registerFile(“Spiffy/Doctrine/Annotations/Zend/Validator.php”);
- Set the default entity manager for Spiffy\Form. \Spiffy\Form::setDefaultEntityManager($em);
Creating an entity
// My/Entity/User.php
namespace My\Entity;
use Spiffy\Entity;
/**
* @ORM\Table(name="user")
* @ORM\Entity
*/
class User extends Entity
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string $username
*
* @ORM\Column(type="string")
*/
private $username;
/**
* @var string $email
*
* @Assert\EmailAddress()
* @ORM\Column(type="string")
*/
private $email;
/**
* @var string $password
*
* @ORM\Column(type="string")
*/
private $password;
public function __toString() {
return "{$this->username}";
}
public function getId() {
return $this->id;
}
public function setId($id) {
$this->id = $id;
}
public function getUsername() {
return $this->username;
}
public function setUsername($username) {
$this->username = $username;
}
public function getEmail() {
return $this->email;
}
public function setEmail($email) {
$this->email = $email;
}
public function getPassword() {
return $this->password;
}
public function setPassword($password) {
$this->password = $password;
}
}
The Form
// My/Forms/Register.php
namespace My\Forms\Register;
use Spiffy\Dojo\Form;
class Register extends Form
{
/**
* (non-PHPdoc)
* @see Zend_Form::init()
*/
public function init() {
$this->setName('register');
$this->add('username');
$this->add('email');
$this->add('password', 'PasswordTextBox');
$this->add('submit', 'SubmitButton');
}
/**
* (non-PHPdoc)
* @see Spiffy.Form::getDefaultOptions()
*/
public function getDefaultOptions() {
return array('entity' => 'Blitzaroo\Entity\User');
}
}
The Controller
// application/controllers/RegisterController.php
use My\Forms\Register;
class RegisterController extends Zend_Controller_Action
{
public function indexAction() {
$form = new Register();
$request = $this->getRequest();
if ($request->isPost()) {
if ($form->isValid($request->getPost())) {
// success!
// user entity
$user = $form->getEntity();
// outputs the value of $form->username
echo $user->getUsername();
}
}
$this->view->form = $form;
}
}
Wrap Up
Well, there’s a small example of what you can do with the new classes. This is not an exhaustive example nor have all the features (Filters) been implemented. Enjoy!