Zend Framework

Improve Zend_Filter_Input capabilities

Details

  • Type: Improvement Improvement
  • Status: Open Open
  • Priority: Major Major
  • Resolution: Unresolved
  • Affects Version/s: 1.8.0
  • Fix Version/s: None
  • Component/s: Zend_Filter_Input
  • Labels:
    None

Description

As recommended by Thomas Weidner, in issue #ZF-6457, i would like to propose this improvement for Zend_Filter_Input. In #ZF-6457, i wrote about problems when using Zend_Filter_Input to filtering and validating operations using only one instance of Zend_Filter_Input, because the order of the things. So, i wrote:

"Ok. Let's make the things clear. A normal register process works the following way:
1 - The user types his name and date of birth in a form. Let's suppose the date field is in format "d/m/Y".
2 - The user clicks on the submit button. Then i use Zend_Filter_Input to make data processing. The problem is: how could i use Zend_Filter_Input to filter and validate this date?"

And Thomas Weidner wrote:

"First: Validate the input and look if it conforms your needs.
Second: Filter the validated and correct value for further processing for your application.

The thing is: It would make no sense to filter/change the input when it's not valid."

Using Zend_Filter_Input in only one execution have some problems. For example, if a user enter with the value (1,500.00) in a number field and submits the form, we could have the following validation rules to apply in Zend_Filter_Input:

$validators = array( 'salary' => array('Float', array('Between', 0, 99999999.99) );

But, making validation before filtering, Float validation will pass (because in 1.8 Zend_Validate_Float supports locale), but the Between validation won't. I'm not sure about the best solution to make these things with only one instance of Zend_Filter_Input, maybe we could define a chain combining filters and validators, something like:

$chain = array(
	'filters' => array(
		'name' => 'StringTrim',
		'biografy' => array('StringTrim', 'StripTags'),
		'birth_date' => 'Date'
	),
	'validators' => array(
		'name' => array('notEmpty', 'Alnum', array('StringLength', 1, 100)),
		'biografy' => array(array('StringLength', 0, 1000)),
		'salary' => 'Float',
		'birth_date' => 'ToDateObject', // Future String to Zend_Date converter
	),
	'filters' => array(
		'salary' => new Zend_Filter_LocalizedToNormalized(array('precision' => 2))
	),
	'validators' => array(
		'salary' => array(array('Between', 0, 99999999.99)),
		'birth_date' => array( 'DateGreaterThan', $date ) // Future Zend_Date object validator
	)
);

$input = Zend_Filter_Input(null, null, $data, $options);
$input->setChain($chain);

Well, that's my idea. The solution can be a bit immature yet. But the idea is that we could have as many as possible data filtering and processing using Zend_Filter_Input.

Activity

Hide
Satoru Yoshida added a comment -

set component and auto reassign

Show
Satoru Yoshida added a comment - set component and auto reassign
Hide
Jon Whitcraft added a comment -

I am wondering if this should be a proposal as it's changing the way the Filter_Input works.

Show
Jon Whitcraft added a comment - I am wondering if this should be a proposal as it's changing the way the Filter_Input works.
Hide
Leandro Rodrigues Chaves added a comment -

Yeah, maybe a new component called Zend_Filter_Input_Chain, or just a conditional in Zend_Filter_Input::process() method to consider a chain of filters and validators.

Show
Leandro Rodrigues Chaves added a comment - Yeah, maybe a new component called Zend_Filter_Input_Chain, or just a conditional in Zend_Filter_Input::process() method to consider a chain of filters and validators.

People

Vote (0)
Watch (1)

Dates

  • Created:
    Updated: