Advertisement
Eddz

Untitled

Sep 3rd, 2013
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. namespace Application\Form;
  2.  
  3. use DoctrineModule\Validator\NoObjectExists as NoObjectExistsValidator;
  4. use Zend\Form\Form;
  5. use Zend\ServiceManager\ServiceManager;
  6.  
  7. class User extends Form
  8. {
  9. public function __construct(ServiceManager $serviceManager)
  10. {
  11. parent::__construct('my-form');
  12.  
  13. // Add an element
  14. $this->add(array(php
  15. 'type' => 'Zend\Form\Element\Email',
  16. 'name' => 'email',
  17. 'options' => array(
  18. 'label' => 'Email'
  19. ),
  20. 'attributes' => array(
  21. 'required' => 'required'
  22. )
  23. ));
  24.  
  25. // add other elements (submit, CSRF…)
  26.  
  27. // Fetch any valid object manager from the Service manager (here, an entity manager)
  28. $entityManager = $serviceManager->get('Doctrine\ORM\EntityManager');
  29.  
  30. // Now get the input filter of the form, and add the validator to the email input
  31. $emailInput = $this->getInputFilter()->get('email');
  32.  
  33. $noObjectExistsValidator = new NoObjectExistsValidator(array(
  34. 'object_repository' => $entityManager->getRepository('Application\Entity\User'),
  35. 'fields' => 'email'
  36. ));
  37.  
  38. $emailInput->getValidatorChain()
  39. ->addValidator($noObjectExistsValidator);
  40. }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement