Validating Collections in Zend Framework2

2019-09-09 15:55发布

问题:

people.. My problem is:

I have a form, with a Collection, so, I setted a CollectionInputFilter to this Collection..

If i send two block of fields (collections) to validate, and one field of one of this block is validated, the same field in the another one block is automatically validated too, even if is wrong or not filled.. I don't know what to do.. I tried a lot of tips.. but, none worked..

Someone can help me? Or faced the same problem?

Here my code:

class ClienteEnderecoFilter extends CollectionInputFilter
{
 public function __construct()
    {
$this->add(array(
            'name' => 'fieldOne',
            'required' => true,
            ));

}
}

If i send two fieldOne and one is filled, the another one is validated too!

回答1:

I think you need to set the input filter for each collection item on to the CollectionInputFilter instead of extending it. Like this:

use Zend\InputFilter\InputFilter;

class ClienteEnderecoFilter extends InputFilter
{
    public function __construct()
    {
        $this->add(array(
            'name' => 'fieldOne',
            'required' => true,
        ));

    }
}

$collectionInputFilter = new CollectionInputFilter();
$collectionInputFilter->setInputFilter(new ClienteEnderecoFilter());