-
-
Save hungtrinh/b501b4af92ce40f7fc29 to your computer and use it in GitHub Desktop.
Best practice form/input-filter setup for ZF2
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 Application\Entity; | |
/** | |
* Class Foo | |
* | |
* @package Application\Entity | |
*/ | |
class Foo | |
{ | |
/** | |
* @var int | |
*/ | |
protected $id; | |
/** | |
* @var string | |
*/ | |
protected $email; | |
/** | |
* @return int | |
*/ | |
public function getId() | |
{ | |
return $this->id; | |
} | |
/** | |
* @return string | |
*/ | |
public function getEmail() | |
{ | |
return $this->email; | |
} | |
/** | |
* @param string $email | |
*/ | |
public function setEmail($email) | |
{ | |
$this->email = $email; | |
} | |
} |
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 Application\Form; | |
use Zend\Form\Element\Text; | |
use Zend\Form\Form; | |
/** | |
* Class FooForm | |
* | |
* @package Application\Form | |
*/ | |
class FooForm extends Form | |
{ | |
const EMAIL = 'email'; | |
/** | |
* {@inheritdoc} | |
*/ | |
public function init() | |
{ | |
$this->add([ | |
'name' => self::EMAIL, | |
'type' => Text::class, | |
'options' => [ | |
'label' => self::EMAIL, | |
] | |
]); | |
} | |
} |
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 Application\Factory\Form; | |
use Application\Entity\Foo; | |
use Application\Form\FooForm; | |
use Application\InputFilter\FooInputFilter; | |
use Zend\ServiceManager\FactoryInterface; | |
use Zend\ServiceManager\ServiceLocatorInterface; | |
use Zend\Stdlib\Hydrator\ClassMethods; | |
/** | |
* Class FooFormFactory | |
* | |
* @package Application\Factory\Form | |
*/ | |
class FooFormFactory implements FactoryInterface | |
{ | |
/** | |
* {@inheritdoc} | |
*/ | |
public function createService(ServiceLocatorInterface $serviceLocator) | |
{ | |
/** | |
* @var \Zend\Form\FormElementManager $serviceLocator | |
* @var \Zend\ServiceManager\ServiceManager $serviceManager | |
*/ | |
$serviceManager = $serviceLocator->getServiceLocator(); | |
$form = new FooForm('foo'); | |
$form->setHydrator(ClassMethods::class); | |
$form->setInputFilter($serviceManager->get('InputFilterManager')->get(FooInputFilter::class)); | |
$form->setObject(new Foo); | |
return $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 Application\InputFilter; | |
use Application\Form\FooForm; | |
use Zend\Filter\StringToLower; | |
use Zend\Filter\StringTrim; | |
use Zend\InputFilter\InputFilter; | |
use Zend\Validator\EmailAddress; | |
/** | |
* Class FooInputFilter | |
* | |
* @package Application\InputFilter | |
*/ | |
class FooInputFilter extends InputFilter | |
{ | |
/** | |
* {@inheritdoc} | |
*/ | |
public function init() | |
{ | |
$this->add([ | |
'name' => FooForm::EMAIL, | |
'required' => true, | |
'filters' => [ | |
['name' => StringToLower::class], | |
['name' => StringTrim::class], | |
], | |
'validators' => [ | |
['name' => EmailAddress::class], | |
] | |
]); | |
} | |
} |
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 | |
use Application\Factory\Form\FooFormFactory; | |
use Application\Form\FooForm; | |
use Application\InputFilter\FooInputFilter; | |
return [ | |
'form_elements' => [ | |
'factories' => [ | |
FooForm::class => FooFormFactory::class, | |
], | |
], | |
'input_filters' => [ | |
'invokables' => [ | |
FooInputFilter::class => FooInputFilter::class, | |
], | |
], | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment