This repository was archived by the owner on Jan 30, 2020. It is now read-only.
This repository was archived by the owner on Jan 30, 2020. It is now read-only.
NotEmpty validator doesn't override the required attribute #14
Closed
Description
A NotEmpty
validator should override the required
check on an input, but in some cases it doesn't. I'm sorry I haven't been able to pin point the exact location of the bug, but here's the code to reproduce it. In test case 1 both NotEmpty
validators overrides the required
attribute, which can be seen by the error messages. In test case 2 however, field2
doesn't use it's NotEmpty
validator but instead falls back to it's internal check to see if the value is empty.
<?php
require __DIR__ . '/vendor/autoload.php';
class MyFilter extends Zend\InputFilter\InputFilter
{
public function init()
{
$this->add([
'name' => 'field1',
'required' => true,
'validators' => [
[
'name' => 'Zend\Validator\NotEmpty',
'break_chain_on_failure' => true,
'options' => [
'messages' => [
'isEmpty' => "Field1 cannot be empty",
],
],
],
],
]);
$this->add([
'name' => 'field2',
'required' => true,
'validators' => [
[
'name' => 'Zend\Validator\NotEmpty',
'break_chain_on_failure' => true,
'options' => [
'messages' => [
'isEmpty' => "Field2 cannot be empty",
],
],
],
],
]);
}
}
$app = Zend\Mvc\Application::init([
'modules' => [
],
'module_listener_options' => [
'module_paths' => [
],
'config_glob_paths' => [
],
],
'input_filters' => [
'invokables' => [
'MyFilter' => 'MyFilter',
],
],
]);
$manager = $app->getServiceManager()->get('InputFilterManager');
$filter = $manager->get('MyFilter');
echo "\n";
echo "#------------------------------\n";
echo "# Test case 1\n";
echo "#------------------------------\n";
echo "\n";
$filter->setData([
]);
if (!$filter->isValid()) {
var_dump($filter->getMessages());
}
echo "\n";
echo "#------------------------------\n";
echo "# Test case 2\n";
echo "#------------------------------\n";
echo "\n";
$filter->setData([
'field1' => [
'test',
],
]);
if (!$filter->isValid()) {
var_dump($filter->getMessages());
}
The output I get is:
#------------------------------
# Test case 1
#------------------------------
array(2) {
'field1' =>
array(1) {
'isEmpty' =>
string(22) "Field1 cannot be empty"
}
'field2' =>
array(1) {
'isEmpty' =>
string(22) "Field2 cannot be empty"
}
}
#------------------------------
# Test case 2
#------------------------------
array(1) {
'field2' =>
array(1) {
[0] =>
string(17) "Value is required"
}
}