Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Feature CollectionInputFilter #3879

Conversation

davidwindell
Copy link
Contributor

Currently, there is no way to directly specify a single InputFilter that can loop and filter a subset of data (for example when using the Collection Element on a form). This makes it near impossible to inject dependencies into custom validators used on collections.

This PR introduces a CollectionInputFilter which allows attaching a single input filter to a collection of data in the following way;

$this->add(array(
    'type' => 'Zend\InputFilter\CollectionInputFilter',
    'inputfilter' => new InvoiceItemFilter()
), 'items');

or

$items = new CollectionInputFilter();
$items->setInputFilter(new InvoiceItemFilter());
$this->add($items, 'items');

@bakura10
Copy link
Contributor

How this will integrate with the already implemented count validation ? https://github.com/zendframework/zf2/blob/master/library/Zend/Form/Element/Collection.php#L179

}

if (is_array($name)) {
if (isset($name[0]) && is_array($name[0])) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'd better do reset($name) instead of $name[0]. This way it will work even if the key is not an integer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is deliberate to prevent any BC breaks in Form which turns the validation group into an array list.

@bakura10
Copy link
Contributor

There is still something I feel a bit weird about this. Semantically, an InputFilter is already a collection of inputs. Therefore, it looks like you are tying this to a specific element in the Form. What is exactly the use case of this ? Being able to specify different input filter for different elements in the same collection ?

@davidwindell
Copy link
Contributor Author

Hi Michael, I'm going to put up some examples next week to demonstrate :) [Done]

@bakura10
Copy link
Contributor

Ok, thanks :).

@davidwindell
Copy link
Contributor Author

@weierophinney this is my take on your suggestion from IRC last Friday. I'd appreciate your comments.

@davidwindell
Copy link
Contributor Author

Ok, tests (including failing ones against amends to existing classes) have now been added so I've removed the WIP flag. This should be good to merge following a code review.

@ghost ghost assigned weierophinney Mar 8, 2013
@weierophinney
Copy link
Member

@davidwindell Closing and re-opening so that the build will be re-triggered.

@weierophinney
Copy link
Member

@davidwindell Can you detail the use case a bit more, in code -- show the type of data set, and why this approach will better address it? (this will also be a nice start to documenting the feature). Once you have, ping @bakura10 so he can better understand and review. So far, though, looks sane.

@romankonz
Copy link
Contributor

This feature is absolutely useful!

Let's say, we have the following mongodb-document for a book:

{
  "_id" : "my book",
  "description" : "an very interesting book",
  "pages" : [
    { "number" : 1, "content" : "placeholder 1" }
    { "number" : 2, "content" : "placeholder 2" }
  ]

using doctrine, we have a class hierarchy like this (pseudo-php):

<?php

class Page
{
  $name
  $placeholder
}

class Book
{
  $number
  $content
  // contains array of pages
  $pages = array()
}

now we want to filter and validate in a restful controller without using the form component.

I've searched this feature today, and will be happy about a merge!

@bakura10
Copy link
Contributor

Thanks. I'll review this more carefully on Friday ('cause I'm giving a ZF 2 training this whole week). Please ping me on IRC for that if I forget ;-).

@@ -233,6 +233,16 @@ public function createInputFilter($inputFilterSpecification)
'Zend\InputFilter\InputFilterInterface', $class));
}

if ($inputFilter instanceof CollectionInputFilter) {
if (isset($inputFilterSpecification['inputfilter'])) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To follow convention, this should be input_filter, not inputfilter. Options are always underscore separated.

@davidwindell
Copy link
Contributor Author

@bakura10 thank you for the review, I've implemented that feedback. Going to update the description of the PR with a more detailed use example now.

weierophinney added a commit that referenced this pull request Mar 25, 2013
weierophinney added a commit that referenced this pull request Mar 25, 2013
@weierophinney
Copy link
Member

Merged, and will release with 2.2.0!

@davidwindell
Copy link
Contributor Author

Thanks @weierophinney!

weierophinney added a commit that referenced this pull request Mar 26, 2013
- Addition of #3893 into develop broke test assumptions in #3879
  Basically, an empty string indicates a value was passed;
  one input, while it was marked as not required, was not marked as
  allow empty, and thus a validator that checked for string length
  failed. Changing the input to allow empty made the tests pass again.
- The question is: if a value is not marked as required, but has an
  empty string passed, should we pass it to the validator?
@svycka
Copy link
Contributor

svycka commented Oct 29, 2013

maybe there is examples or any documentation how to use this?

@sebob
Copy link

sebob commented Dec 10, 2013

@bakura10 ping

weierophinney added a commit to zendframework/zend-inputfilter that referenced this pull request May 15, 2015
…/feature-collection-input-filter

Feature CollectionInputFilter
weierophinney added a commit to zendframework/zend-inputfilter that referenced this pull request May 15, 2015
weierophinney added a commit to zendframework/zend-inputfilter that referenced this pull request May 15, 2015
- Addition of zendframework/zendframework#3893 into develop broke test assumptions in zendframework/zendframework#3879
  Basically, an empty string indicates a value was passed;
  one input, while it was marked as not required, was not marked as
  allow empty, and thus a validator that checked for string length
  failed. Changing the input to allow empty made the tests pass again.
- The question is: if a value is not marked as required, but has an
  empty string passed, should we pass it to the validator?
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants