New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[hotfix/ZF-11617] Zend_Filter_StripTags #285

Closed
wants to merge 2 commits into
base: master
from
Jump to file or symbol
Failed to load files and symbols.
+130 −1
Diff settings

Always

Just for now

Next

[hotfix/ZF-9289] Zend_Filter_InputFilter

- setting ALLOW_EMPTY should not ignore specific NotEmpty rules
  • Loading branch information...
thomasweidner committed Jul 27, 2011
commit 25a4c41b58a9ec620cf11969d137a8126ba1dd40
@@ -761,7 +761,33 @@ protected function _validate()
$validatorRule[self::PRESENCE] = $this->defaults[self::PRESENCE];
}
if (!isset($validatorRule[self::ALLOW_EMPTY])) {
$validatorRule[self::ALLOW_EMPTY] = $this->defaults[self::ALLOW_EMPTY];
$foundNotEmptyValidator = false;
foreach ($validatorRule as $rule) {
if ($rule === 'NotEmpty') {
$foundNotEmptyValidator = true;
// field may not be empty, we are ready
break 1;
}
// we must check if it is an object before using instanceof
if (!is_object($rule)) {
// it cannot be a NotEmpty validator, skip this one
continue;
}
if($rule instanceof Validator\NotEmpty) {
$foundNotEmptyValidator = true;
// field may not be empty, we are ready
break 1;
}
}
if (!$foundNotEmptyValidator) {
$validatorRule[self::ALLOW_EMPTY] = $this->defaults[self::ALLOW_EMPTY];
} else {
$validatorRule[self::ALLOW_EMPTY] = false;
}
}
if (!isset($validatorRule[self::MESSAGES])) {
@@ -1859,6 +1859,109 @@ public function testNoNestedMessageArrays()
$this->assertFalse(is_array($messages['field1']['isEmpty']), 'oh oh, we may have got nested messages');
$this->assertTrue(isset($messages['field1']['isEmpty']), 'oh no, we not even got the normally expected messages');
}
/**
* If setAllowEmpty(true) is called, all fields are optional, but fields with
* a NotEmpty validator attached to them, should contain a non empty value.
*
* @group ZF-9289
*/
function testAllowEmptyTrueRespectsNotEmtpyValidators()
{
$data = array(
'field1' => 'foo',
'field2' => ''
);
$validators = array(
'field1' => array(
new Validator\NotEmpty(),
InputFilter::MESSAGES => array(
array(
Validator\NotEmpty::IS_EMPTY => '\'field1\' is required'
)
)
),
'field2' => array(
new Validator\NotEmpty()
)
);
$options = array(InputFilter::ALLOW_EMPTY => true);
$input = new InputFilter( null, $validators, $data, $options );
$this->assertFalse($input->isValid(), 'Ouch, the NotEmpty validators are ignored!');
$validators = array(
'field1' => array(
'Digits',
array('NotEmpty', 'integer'),
InputFilter::MESSAGES => array(
1 =>
array(
Validator\NotEmpty::IS_EMPTY => '\'field1\' is required'
)
),
),
);
$data = array(
'field1' => 0,
'field2' => ''
);
$options = array(InputFilter::ALLOW_EMPTY => true);
$input = new InputFilter( null, $validators, $data, $options );
$this->assertFalse($input->isValid(), 'Ouch, if the NotEmpty validator is not the first rule, the NotEmpty validators are ignored !');
// and now with a string 'NotEmpty' instead of an instance:
$validators = array(
'field1' => array(
'NotEmpty',
InputFilter::MESSAGES => array(
0 =>
array(
Validator\NotEmpty::IS_EMPTY => '\'field1\' is required'
)
),
),
);
$data = array(
'field1' => '',
'field2' => ''
);
$options = array(InputFilter::ALLOW_EMPTY => true);
$input = new InputFilter( null, $validators, $data, $options );
$this->assertFalse($input->isValid(), 'If the NotEmpty validator is a string, the NotEmpty validator is ignored !');
// and now with an array
$validators = array(
'field1' => array(
array('NotEmpty', 'integer'),
InputFilter::MESSAGES => array(
0 =>
array(
Validator\NotEmpty::IS_EMPTY => '\'field1\' is required'
)
),
),
);
$data = array(
'field1' => 0,
'field2' => ''
);
$options = array(InputFilter::ALLOW_EMPTY => true);
$input = new InputFilter( null, $validators, $data, $options );
$this->assertFalse($input->isValid(), 'If the NotEmpty validator is an array, the NotEmpty validator is ignored !');
}
}
} // end namespace declaration
ProTip! Use n and p to navigate between commits in a pull request.