Advertisement
Guest User

Form

a guest
Sep 23rd, 2014
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Application\Form;
  4.  
  5. use Zend\InputFilter;
  6. use Zend\Form\Element;
  7. use Zend\Form\Form;
  8.  
  9. class UploadForm extends Form {
  10.  
  11. public function __construct($name = null, $options = array()) {
  12. parent::__construct($name, $options);
  13. $this->addElements();
  14. $this->addInputFilter();
  15. }
  16.  
  17. public function addElements() {
  18. // File Input
  19. $file = new Element\File('image-file');
  20. $file->setLabel('Avatar Image Upload')
  21. ->setAttribute('id', 'image-file')
  22. ->setAttribute('multiple', true); // That's it
  23. $this->add($file);
  24. }
  25.  
  26. public function addInputFilter() {
  27. $inputFilter = new InputFilter\InputFilter();
  28.  
  29. // File Input
  30. $fileInput = new InputFilter\FileInput('image-file');
  31. $fileInput->setRequired(true);
  32.  
  33. // You only need to define validators and filters
  34. // as if only one file was being uploaded. All files
  35. // will be run through the same validators and filters
  36. // automatically.
  37. $fileInput->getValidatorChain()
  38. // ->attachByName('filesize', array('max' => 204800))
  39. ->attachByName('filemimetype', array('mimeType' => 'image/png,image/x-png'))
  40. // ->attachByName('fileimagesize', array('maxWidth' => 100, 'maxHeight' => 100))
  41. ;
  42.  
  43. // All files will be renamed, i.e.:
  44. // ./data/tmpuploads/avatar_4b3403665fea6.png,
  45. // ./data/tmpuploads/avatar_5c45147660fb7.png
  46. $fileInput->getFilterChain()->attachByName(
  47. 'filerenameupload', array(
  48. 'target' => './data/tmpuploads/avatar.png',
  49. 'randomize' => true,
  50. )
  51. );
  52. $inputFilter->add($fileInput);
  53.  
  54. $this->setInputFilter($inputFilter);
  55. }
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement