ZF-6763: Zend_Filter_Input doesn't allow to use arrays as a default value for specified field.

Description

Zend_Filter_input allows to set default value, used for missing fields.

If value is scalar, it's used as a default value for all fields. If value is an array, it's used as => pairs (see Zend_Filter_Input::_validateRule() for details).

So it's not possible to use arrays as a default value for all rule fields even it's one field within a rule.

My offer is to use the following algorithm: if rule has only one field, default value is used even it's an array or not. if rule has several fields, scalar values used as a default values for all fields array is used as a list of default values and special pair '*' => is used as a default field value for all rule fields, => pair overrides universal default rule:


// field2 and field3 will be set to $value2
$data = array('field1' => $value1);
$validators = array(
    'ruleq'   => array(
        'MyCoolValidator',
        Zend_Filter_Input::self::FIELDS => array('field1', 'field2', 'field3')
        Zend_Filter_Input::self::DEFAULT_VALUE => $value2;
    )
);

// field2 and field3 will be set to $value2
$data = array('field1' => $value1);
$validators = array(
    'ruleq'   => array(
        'MyCoolValidator',
        Zend_Filter_Input::self::FIELDS => array('field1', 'field2', 'field3')
        Zend_Filter_Input::self::DEFAULT_VALUE => array('*' => $value2);
    )
);

// field2 and field3 will be set to array($value2)
$data = array('field1' => $value1);
$validators = array(
    'ruleq'   => array(
        'MyCoolValidator',
        Zend_Filter_Input::self::FIELDS => array('field1', 'field2', 'field3')
        Zend_Filter_Input::self::DEFAULT_VALUE => array('*' => array($value2));
    )
);

// field2 will be set to $value2, field1 and field3 will be set to $value3
$data = array();
$validators = array(
    'ruleq'   => array(
        'MyCoolValidator',
        Zend_Filter_Input::self::FIELDS => array('field1', 'field2', 'field3')
        Zend_Filter_Input::self::DEFAULT_VALUE => array('field2' => $value2, '*' => $value3);
    )
);

Comments

No comments to display