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

fixxed #94 'Missing required failure message for CollectionInputFilter' issue #170

Merged
merged 4 commits into from
Dec 17, 2018

Conversation

ruzann
Copy link
Contributor

@ruzann ruzann commented Jul 18, 2018

Added default message to display when a collection is required.
Added mechanism for setting a custom "required" message.
Updated factorie to allow setting that message.

src/CollectionInputFilter.php Outdated Show resolved Hide resolved
src/CollectionInputFilter.php Outdated Show resolved Hide resolved
@ruzann ruzann changed the title fixxed 'Missing required failure message for CollectionInputFilter' issue fixxed #94 'Missing required failure message for CollectionInputFilter' issue Jul 18, 2018
src/CollectionInputFilter.php Outdated Show resolved Hide resolved
src/CollectionInputFilter.php Outdated Show resolved Hide resolved
src/CollectionInputFilter.php Outdated Show resolved Hide resolved
src/CollectionInputFilter.php Outdated Show resolved Hide resolved
src/CollectionInputFilter.php Outdated Show resolved Hide resolved
@ruzann
Copy link
Contributor Author

ruzann commented Jul 26, 2018

@froschdesign Is there any updates relation to pull request?

@froschdesign
Copy link
Member

@ruzann
I still see a problem here, because your changes introduce a hard coded error message to an input-filter, that means we have message constants in input-filters and validators.
This ends in another problem, because the error message can not be translated.

composer.json Outdated Show resolved Hide resolved
@froschdesign
Copy link
Member

@ruzann
Do not create a new translator object, this will introduce new problems and you must set the the translation files for the input-filter - this is wrong!
Please revert this and use the translator from the validator.

@ruzann
Copy link
Contributor Author

ruzann commented Aug 7, 2018

@froschdesign can you please review?

@ruzann
Copy link
Contributor Author

ruzann commented Aug 9, 2018

@froschdesign all your notes fixed, please check again, when you have a time.

@newdevonair
Copy link

@froschdesign is there any news from this pull request?
I need this functionality in my project too.

Thanks.

@froschdesign
Copy link
Member

froschdesign commented Aug 31, 2018

@ruzann
Some pseudo code:

function getNotEmptyValidator()
{
    if ($this->notEmptyValidator === null) {
        $this->notEmptyValidator = new NotEmpty();
    }

    return $this->notEmptyValidator;
}

function setNotEmptyValidator($notEmptyValidator);

function prepareRequiredValidationFailureMessage();

The default error message already exists and you can replace the validator with your own. messageRequired and the related methods are not needed.

(Yes, we do not use the validator for validation, but we allow a translation of the error message with the standard way.)

What do you think?

/cc @malukenho @rkeet

@newdevonair
You can also add a review or a comment to the current suggestions. 😉


Sorry for the late response and the short review. My focus is currently on the documentation, website and zend-navigation.

@rkeet
Copy link
Contributor

rkeet commented Aug 31, 2018

I would suggest same as @froschdesign review: own getter/setter for storing NotEmpty instance.

Otherwise seems alright to me, apart from tests not passing ;)

@ruzann
Copy link
Contributor Author

ruzann commented Sep 24, 2018

@froschdesign I fixed your notes, please check again.

@tigran-m-dev
Copy link

Hi guys, do you have any updates related this issue? Is it ready to merge or have any other improvement?
If you need some help I am ready to help.

Copy link
Member

@weierophinney weierophinney left a comment

Choose a reason for hiding this comment

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

Honestly, this looks really convoluted. If I'm reading this correctly:

  • You allow injecting a string message for the purposes of seeding a NotEmpty validator
  • You manually test if the collection is empty, and, if so:
    • grab the NotEmpty validator
    • Grab its NOT_EMPTY template to use as a message
    • Optionally translate the message, if the validator has a translator attached
    • Return that message

It seems like it would make more sense to:

  • Allow setting a message template for when the collection is empty
  • Allow optionally setting a translator, to allow translating the above message
  • Use each of the above when the collection is empty to populate the validation failure messages

Alternately, we should allow specifying a validator and/or validator chain at the top level for checking the status of the collection, prior to attempting to validate the members of the collection.

@froschdesign
Copy link
Member

@weierophinney

Alternately, we should allow specifying a validator and/or validator chain at the top level for checking the status of the collection, prior to attempting to validate the members of the collection.

This one! (See my example with pseudo code above.)
Why? In all inputs we uses the messages from the validator. Setting a message to an input would introduce a new behaviour for this component. Setting a validator or a validator chain is not new.
The validator has already the option to translate a message.

@ruzann
Copy link
Contributor Author

ruzann commented Nov 12, 2018

@weierophinney, @froschdesign is there any news?
should I change something?

@weierophinney
Copy link
Member

@ruzann Please see the feedback above - @froschdesign and I feel that the approach outlined here is convoluted, and inconsistent with the architecture. Adding a validator chain specific to the collection input filter as an entity would make more sense. If you can refactor to that approach, we can review again.

src/CollectionInputFilter.php Outdated Show resolved Hide resolved
src/Factory.php Outdated
@@ -319,6 +319,9 @@ public function createInputFilter($inputFilterSpecification)
if (isset($inputFilterSpecification['required'])) {
$inputFilter->setIsRequired($inputFilterSpecification['required']);
}
if (isset($inputFilterSpecification['messageRequired'])) {
Copy link
Member

Choose a reason for hiding this comment

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

Please use required_message for consistency.

@froschdesign froschdesign added this to the 2.9.0 milestone Nov 15, 2018
@ruzann
Copy link
Contributor Author

ruzann commented Nov 28, 2018

@froschdesign is there any news?

@froschdesign
Copy link
Member

@ruzann
Your improvement will be included in the next minor release (2.9). At the moment I am working at documentation of zend-hydrator, therefore I can not give a time frame. I'm sorry.

In the meantime, just use your own fork.

ruzann and others added 2 commits December 17, 2018 10:16
This patch modifies the `CollectionInputFilter` to allow injecting a
`Zend\Validator\NotEmpty` instance. When the collection is marked
"required", but receives an empty data set, it will use that validator
to provide a validation failure message. (If no validator was set, it
lazy-loads one.)

When using factory-based collections, you may now also provide a
`required_message` key as a sibling to `required`; when set, this
message will be used in place of the default `NotEmpty` validation error
message.
- Combine conditionals, as there is only one combination that has a meaning
- Use a ternary to allow removing a transient variable
Copy link
Member

@weierophinney weierophinney left a comment

Choose a reason for hiding this comment

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

The changes look much better, @ruzann .

I'm going to do the following:

  • Rebase off of develop
  • Squash your commits (50 is excessive here!)
  • Split the tests up to demonstrate the behaviors we're interested in
  • Push a few additional changes I noticed during review

Then we can 🚢!

test/CollectionInputFilterTest.php Show resolved Hide resolved
src/CollectionInputFilter.php Show resolved Hide resolved
src/CollectionInputFilter.php Outdated Show resolved Hide resolved
- Lazy-loads a NotEmpty validator if none provided.
- Allows composing a NotEmpty validator.
- Uses the message from a composed NotEmpty validator when validating a
  required, but empty, collection.
- Factory will use the `required_message` key to seed the `NotEmpty`
  validator of a collection.
@weierophinney weierophinney changed the base branch from master to develop December 17, 2018 16:56
@weierophinney weierophinney merged commit 9fd3546 into zendframework:develop Dec 17, 2018
@weierophinney
Copy link
Member

Thanks, @ruzann!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants