Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Application\Form;
- use DoctrineModule\Validator\NoObjectExists as NoObjectExistsValidator;
- use Zend\Form\Form;
- use Zend\ServiceManager\ServiceManager;
- class User extends Form
- {
- public function __construct(ServiceManager $serviceManager)
- {
- parent::__construct('my-form');
- // Add an element
- $this->add(array(php
- 'type' => 'Zend\Form\Element\Email',
- 'name' => 'email',
- 'options' => array(
- 'label' => 'Email'
- ),
- 'attributes' => array(
- 'required' => 'required'
- )
- ));
- // add other elements (submit, CSRF…)
- // Fetch any valid object manager from the Service manager (here, an entity manager)
- $entityManager = $serviceManager->get('Doctrine\ORM\EntityManager');
- // Now get the input filter of the form, and add the validator to the email input
- $emailInput = $this->getInputFilter()->get('email');
- $noObjectExistsValidator = new NoObjectExistsValidator(array(
- 'object_repository' => $entityManager->getRepository('Application\Entity\User'),
- 'fields' => 'email'
- ));
- $emailInput->getValidatorChain()
- ->addValidator($noObjectExistsValidator);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement