zendframework / zend-inputfilter

InputFilter component from Zend Framework
BSD 3-Clause "New" or "Revised" License
64 stars 50 forks source link

CollectionInputFilter wrong result messages #114

Closed hopsey closed 8 years ago

hopsey commented 8 years ago

Let's say I have an InputFilter

use Zend\InputFilter\InputFilter;
use Zend\Filter\StringTrim;
use Zend\Filter\StripTags;
use Zend\InputFilter\Input;
use Zend\Validator\EmailAddress;
use Zend\Validator\NotEmpty;

class MyInputFilter extends InputFilter
{
    public function __construct()
    {
        $email = new Input('email');
        $email->setRequired(true);
        $email->getFilterChain()
            ->attach(new StringTrim())
            ->attach(new StripTags());

        $email->getValidatorChain()
            ->attach(new EmailAddress())
            ->attach(new NotEmpty());
        $this->add($email);

        $name = new Input('name');
        $name->setRequired(true);
        $name->getFilterChain()
            ->attach(new StringTrim())
            ->attach(new StripTags());

        $name->getValidatorChain()
            ->attach(new NotEmpty());
        $this->add($name);
    }
}

and the logic that goes

use Zend\InputFilter\CollectionInputFilter;

$collection = new CollectionInputFilter();
$collection->setInputFilter(new MyInputFilter());
$collection->setData([
    [
        'name' => 'Tom',
    ],
    [
        'email' => 'tom@tom',
        'name' => 'Tom'
    ],
]);
var_dump($collection->isValid(), $collection->getMessages());

I get

bool(false)
array(2) {
  [0]=>
  array(1) {
    ["email"]=>
    array(1) {
      ["isEmpty"]=>
      string(36) "Value is required and can't be empty" // which is fine here
    }
  }
  [1]=>
  array(1) {
    ["email"]=>
    array(1) {
      ["isEmpty"]=>
      string(36) "Value is required and can't be empty" // which is not fine
    }
  }
}  

second's element email message should be

    array(3) {
      ["emailAddressInvalidHostname"]=>
      string(53) "'tom' is not a valid hostname for the email address"
      ["hostnameInvalidHostname"]=>
      string(66) "The input does not match the expected structure for a DNS hostname"
      ["hostnameLocalNameNotAllowed"]=>
      string(84) "The input appears to be a local network name but local network names are not allowed"
    }

looks like a bug.