Zf2 How to pass arguments using a filter class?


I would like to use the SeparatorToSeparator () filter in zend framwork 2 to filter my data.
How to pass two arguments (setSearchSeparator and setReplacementSeparator) to the constructor?

$inputFilter->add(array( 'name' => 'supplierName', 'required' => true, 'filters' => array( array('name' => 'StripTags'), array('name' => 'StringTrim'), array('name'=>"Word\SeparatorToSeparator" ), )); 

Path: ZendFramework / bin / libary / Zend / Filter / Word / SeparatorToSeparator.php

 class SeparatorToSeparator extends AbstractFilter { protected $searchSeparator = null; protected $replacementSeparator = null; /** * Constructor * * @param string $searchSeparator Separator to search for * @param string $replacementSeparator Separator to replace with */ public function __construct($searchSeparator = ' ', $replacementSeparator = '-') { $this->setSearchSeparator($searchSeparator); $this->setReplacementSeparator($replacementSeparator); } 

Update

 $inputFilter->add(array( 'name' => 'supplierName', 'required' => true, 'filters' => array( array('name' => 'StripTags'), array('name' => 'StringTrim'), array('name'=>'Word\SeparatorToSeparator', 'options' => array( 'search_separator' => 'a', 'replacement_separator' => 'b' ) ) ), )); 

I got this error message:

Warning: preg_quote () expects parameter 1 to be a string, the array is specified in C: \ WAMP \ WWW \ tebipdevelopment \ provider \ ZendFramework \ ZendFramework \ Library \ Zend \ Filter \ Word \ SeparatorToSeparator.php on line 92

I opened this line and I printed the error message as follows.

 print_r($this->searchSeparator); print_r($this->replacementSeparator); 

Result

Array ([search_separator] => a [replacement_separator] => b)

In this case, search_separator is equal to an array instead of a string

+4
source share
2 answers

Note that you do not need setters, but I added them anyway, the filter will try to use installers if they exist (notation setCamelCase ()).

 class SeparatorToSeparator extends AbstractFilter { protected $searchSeparator = null; protected $replacementSeparator = null; public function setSearchSeparator($val) { $this->searchSeparator = $val; } public function setReplacementSeparator($val) { $this->replacementSeparator = $val; } } 

Now you can set the following parameters:

 $inputFilter->add(array( 'name' => 'supplierName', 'required' => true, 'filters' => array( array('name' => 'StripTags'), array('name' => 'StringTrim'), array( 'name'=>"Word\SeparatorToSeparator" 'options' => array( 'search_separator' => 'bla', 'replacement_separator' => 'bla' ) ) ), )); 
+3
source

I faced the same problem, unlike other validators, the words validators do not accept an array of parameters, as you already found. The workaround I used was to first create an instance of the word filter, passing it the necessary constructor parameters, and then add this instance to the filter specification ...

 $wordFilter = new \Zend\Filter\Word\SeparatorToSeparator('a', 'b'); $inputFilter->add(array( 'name' => 'supplierName', 'required' => true, 'filters' => array( array('name' => 'StripTags'), array('name' => 'StringTrim'), $wordFilter, ), )); 
+1
source

Source: https://habr.com/ru/post/1494225/


All Articles