zend framework2 - getdata on File input to return only filename, not array of details -


i have form has fieldset file upload field in it. when var_dump on $form->getdata() being shown array of data file field:

array (size=13)   'logo' =>      array (size=5)       'name' => string 'my-image.gif' (length=12)       'type' => string 'image/gif' (length=9)       'tmp_name' => string 'c:\xampp\htdocs\images\my-image.gif' (length=35)       'error' => int 0       'size' => int 391   //... other fields here 

how element return name when call getdata?

e.g.

array (size=13)   'logo' => string 'my-image.gif' (length=12)   //... other fields here 

i using form other things , have overridden getdata keep answer located in fieldset.

you can override getdata() method in form.

public function getdata() {     $data = parent::getdata();     $logo = $data['logo'];     $data['logo'] = $logo['name'];     return $data; } 

add necessary precautions ensure existence of keys in arrays.

supplements fieldset

using fileset, can use filter change return file structure :

namespace your\namespace;  use zend\filter;  class filterfilename extends filter\abstractfilter { public function filter($value) {         if (! is_scalar($value) && ! is_array($value)) {             return $value;         }         if (is_array($value)) {             if (! isset($value['name'])) {                 return $value;             }             $return = $value['name'];         } else {             $return = $value;         }         return $return;     } } 

your fieldset class must implement inputfilterproviderinterface

use your\namespace\filterfilename;  class yourfieldset extends zendfiedset implements inputfilterproviderinterface {     public function __construct()     {         // code ... :         parent::__construct('logo');          $file_element = new element\file('my-element-file');         $file_element->setlabel('chooze')             ->setattribute('id', 'my-element-file')             ->setoption('error_attributes', [                'class' => 'form-error'         ]);         $this->add($file_element);                 }     public function getinputfilterspecification()     {         return [             'element-file' => [                 'name' => 'my-element-file',                 'filters' => [                     ['name' => filterfilename::class]                 ]             ]         ];     } } 

you can chain multiple filters, eg rename file before.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -