-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Feature CollectionInputFilter #3879
Feature CollectionInputFilter #3879
Conversation
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])) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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 ? |
Hi Michael, I'm going to put up some examples next week to demonstrate :) [Done] |
Ok, thanks :). |
@weierophinney this is my take on your suggestion from IRC last Friday. I'd appreciate your comments. |
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. |
@davidwindell Closing and re-opening so that the build will be re-triggered. |
@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. |
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! |
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'])) { |
There was a problem hiding this comment.
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.
@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. |
…ilter Feature CollectionInputFilter
Merged, and will release with 2.2.0! |
Thanks @weierophinney! |
- 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?
maybe there is examples or any documentation how to use this? |
@bakura10 ping |
…/feature-collection-input-filter Feature CollectionInputFilter
- 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?
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;
or