Advertisement
Eddz

Untitled

Sep 3rd, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.52 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Fcm\Form;
  4.  
  5. use Zend\Form\Form;
  6. use Zend\Form\Element;
  7. use DoctrineModule\Validator\NoObjectExists as NoObjectExistsValidator;
  8.  
  9. class ClientAdd extends Form
  10. {
  11.  
  12.     protected $repository;
  13.  
  14.     public function __construct($repository)
  15.     {
  16.         $this->repository = $repository;
  17.  
  18.         parent::__construct();
  19.         $this->setName('add-client');
  20.         $this->setAttribute('action', '/clients/add');
  21.         $this->setAttribute('method', 'post');
  22.  
  23.  
  24.         //Fieldset One
  25.         $this->add(array(
  26.             'name' => 'fsOne',
  27.             'type' => 'Zend\Form\Fieldset',
  28.             'options' => array(
  29.                 'legend' => 'Client informations',
  30.             ),
  31.             'elements' => array(
  32.                 // Name
  33.                 array(
  34.                     'spec' => array(
  35.                         'name' => 'name',
  36.                         'type' => 'Zend\Form\Element\Text',
  37.                         'attributes' => array(
  38.                         ),
  39.                         'options' => array(
  40.                             'label' => 'Client name',
  41.                         ),
  42.                     ),
  43.                 ),
  44.                 // Website
  45.                 array(
  46.                     'spec' => array(
  47.                         'name' => 'website',
  48.                         'type' => 'Zend\Form\Element\Text',
  49.                         'attributes' => array(
  50.                         ),
  51.                         'options' => array(
  52.                             'label' => 'Website',
  53.                             'prependText' => 'http://',
  54.                         ),
  55.                     ),
  56.                 ),
  57.                 // Contact
  58.                 array(
  59.                     'spec' => array(
  60.                         'name' => 'contact',
  61.                         'type' => 'Zend\Form\Element\Text',
  62.                         'attributes' => array(
  63.                         ),
  64.                         'options' => array(
  65.                             'label' => 'Contact client',
  66.                         ),
  67.                     ),
  68.                 ),
  69.                 // Email
  70.                 array(
  71.                     'spec' => array(
  72.                         'name' => 'email',
  73.                         'type' => 'Zend\Form\Element\Email',
  74.                         'attributes' => array(
  75.                             'required' => 'required'
  76.                         ),
  77.                         'options' => array(
  78.                             'label' => 'Email',
  79.                         ),
  80.                     ),
  81.                 ),
  82.                 // Phone
  83.                 array(
  84.                     'spec' => array(
  85.                         'name' => 'phone',
  86.                         'type' => 'Zend\Form\Element\Text',
  87.                         'attributes' => array(
  88.                         ),
  89.                         'options' => array(
  90.                             'label' => 'Phone number',
  91.                         ),
  92.                     ),
  93.                 ),
  94.                 // Comment
  95.                 array(
  96.                     'spec' => array(
  97.                         'name' => 'comment',
  98.                         'type' => 'Zend\Form\Element\Textarea',
  99.                         'attributes' => array(
  100.                         ),
  101.                         'options' => array(
  102.                             'label' => 'Comment',
  103.                         ),
  104.                     ),
  105.                 ),
  106.             ),
  107.         ));
  108.  
  109.         //Csrf
  110.         $this->add(new Element\Csrf('csrf'));
  111.  
  112.         //Submit button
  113.         $this->add(array(
  114.             'name' => 'submitBtn',
  115.             'type' => 'Zend\Form\Element\Submit',
  116.             'attributes' => array(
  117.                 'value' => 'Add client',
  118.             ),
  119.             'options' => array(
  120.                 'primary' => true,
  121.             ),
  122.         ));
  123.  
  124.         //Reset button
  125.         $this->add(array(
  126.             'name' => 'resetBtn',
  127.             'attributes' => array(
  128.                 'type' => 'reset',
  129.                 'value' => 'Reset',
  130.             ),
  131.         ));
  132.        
  133.          // Add an element
  134.         $this->add(array(
  135.             'type'    => 'Zend\Form\Element\Email',
  136.             'name'    => 'email',
  137.             'options' => array(
  138.                 'label' => 'Email'
  139.             ),
  140.             'attributes' => array(
  141.                 'required'  => 'required'
  142.             )
  143.         ));
  144.  
  145. //         Now get the input filter of the form, and add the validator to the email input
  146.         $emailInput = $this->getInputFilter()->get('email');
  147.  
  148.         $noObjectExistsValidator = new NoObjectExistsValidator(array(
  149.             'object_repository' => $this->repository,
  150.             'fields' => 'email'
  151.         ));
  152.  
  153.         $emailInput->getValidatorChain()
  154.                 ->addValidator($noObjectExistsValidator);
  155.     }
  156.  
  157. //    public function getInputFilterSpecification()
  158. //    {
  159. ////        die(var_dump($this->serviceManager->getRepository('Fcm\Entity\Client')->checkIfEmailExists("[email protected]"))); // !!! WORKS
  160. //        return array(
  161. //            'email' => array(
  162. //                'validators' => array(
  163. //                    array(
  164. //                        'name' => 'DoctrineModule\Validator\NoObjectExists',
  165. //                        'options' => array(
  166. //                            'object_manager' => $this->repository,
  167. //                            'fields' => 'email'
  168. //                        )
  169. //                    )
  170. //                )
  171. //            )
  172. //        );
  173. //    }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement