I have a few forms with file upload elements. Instead of hardcoding the
'target' path for Zend\Filter\File\RenameUpload, I wish to pull the path
from somewhere (database, config, etc.) and inject it.
// module.config.php    'filters' => array(        'factories' => array(        
   
'Zend\Filter\File\RenameUpload' => function ($sm) {                return
new \Zend\Filter\File\RenameUpload($_SERVER['DOCUMENT_ROOT'] . '/files');       
    
},        ),    ),// form    $element = new \Zend\Form\Element\File('file');   
$element->setLabel('file');    $this->add($element);        $inputFilter =
new \Zend\InputFilter\InputFilter();    $fileInput = new
\Zend\InputFilter\FileInput('file');    $fileInput->setRequired(true);   
$fileInput->getFilterChain()->attachByName(       
'Zend\Filter\File\RenameUpload', // same result as 'filerenameupload'       
array(            // 'target' => '', // commented out to see if factory
works        )    );    $inputFilter->add($fileInput);       
$this->setInputFilter($inputFilter);
I get an error "Warning: Missing argument 1 for
Zend\Filter\File\RenameUpload::__construct()", ie. `target` is not set.
A similar thread was found  here
<http://zend-framework-community.634137.n4.nabble.com/Problem-with-validator-factory-td4659596.html>
  
but it does not fully answer my question.

The filter is never created via the factory so `target` is never set.
Tracing the code for the  method `attachByName`, it seems to check with the
service manager but the factory is not used. Am I doing this correctly or do
I have to make my Form ServiceLocatorAware and then do the following?
$filter =
$this->getServiceLocator()->get('FilterManager')->get('Zend\Filter\File\RenameUpload');$fileInput->getFilterChain()->attach($filter);
Appreciate any feedback and advice, thanks!



-----
Zion Ng
Singapore
--
View this message in context: 
http://zend-framework-community.634137.n4.nabble.com/How-to-inject-dependencies-into-filters-via-config-tp4660772.html
Sent from the Zend Framework mailing list archive at Nabble.com.

Reply via email to