Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS validation: dynamically added form inputs are now validated #260

Merged

Conversation

boris-brtan
Copy link
Contributor

Q A
Description, reason for the PR problem with saving forms with dynamically added parameters
New feature No
BC breaks No
Fixes issues #190
Standards and tests pass Yes
Have you read and signed our License Agreement for contributions? Yes

@boris-brtan boris-brtan force-pushed the bb-admin-product-detail-parameters-js-validation branch 2 times, most recently from c944a3a to 3038ada Compare June 18, 2018 14:47
@boris-brtan boris-brtan force-pushed the bb-admin-product-detail-parameters-js-validation branch 3 times, most recently from d04c194 to 26c595d Compare June 19, 2018 11:28
Copy link
Contributor

@vitek-rostislav vitek-rostislav left a comment

Choose a reason for hiding this comment

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

Hi @boris-brtan, the review is done now, I have a few notes so please check them out. I really do not know what to do with the last commit so I asked @PetrHeinz for help.

CHANGELOG.md Outdated
@@ -60,6 +60,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- changed domain icon in e-shop domain administration can be saved
- [#260 - Admin: Js validation: dynamically added parameters are now validated](https://github.com/shopsys/shopsys/pull/260)
- validation is evaluated after dynamic parameters are added
- jQuery migration console error logging is no more
Copy link
Contributor

Choose a reason for hiding this comment

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

I do not understand this commit at all, what was the problem?

@@ -4,7 +4,10 @@
Shopsys.validation = Shopsys.validation || {};

Shopsys.validation.addNewItemToCollection = function (collectionSelector, itemIndex) {
$($(collectionSelector)).jsFormValidator('addPrototype', itemIndex);
$(collectionSelector).jsFormValidator('addPrototype', itemIndex);
Copy link
Contributor

Choose a reason for hiding this comment

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

why you changed the double cast?

});

function invalidateForm () {
Copy link
Contributor

Choose a reason for hiding this comment

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

this should rather be named to something like "disableJsFormValidation" as it disables JS validation on a form.

.toggle();
}

Shopsys.validation.refreshBindings = function () {
Copy link
Contributor

Choose a reason for hiding this comment

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

We have Shopsys.register component for that purpose - you can register a callback and that register newly added content on which the callback will be executed, see https://github.com/shopsys/shopsys/blob/master/packages/framework/src/Resources/scripts/common/register.js. I think this could serve for the purpose perfectly.

@@ -4,7 +4,10 @@
Shopsys.validation = Shopsys.validation || {};

Shopsys.validation.addNewItemToCollection = function (collectionSelector, itemIndex) {
$($(collectionSelector)).jsFormValidator('addPrototype', itemIndex);
$(collectionSelector).jsFormValidator('addPrototype', itemIndex);
if (Shopsys.validation.refreshBindings instanceof Function) {
Copy link
Contributor

Choose a reason for hiding this comment

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

is this necessary?

/**
* @param int $productId
*/
public function saveInvalidForm($productId)
Copy link
Contributor

Choose a reason for hiding this comment

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

I would name this method more specifically - it saves form with invalid parameter...

$productEditPage->saveInvalidForm(5);
$productEditPage->assertSaveFormErrorBoxVisible();

$productEditPage->cleanUpWorkspaceAfterChanges();
Copy link
Contributor

Choose a reason for hiding this comment

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

why? could not you just call $this->tester->acceptPopup()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In this test the form is changed and browser will stop execution of next tests due to modal box that wants approval. Seems that opposite order of commands doesn't work if the test is ended and next test requests for different page modal box is not canceled.

LoginPage $loginPage,
ProductEditPage $productEditPage
) {
$me->wantTo('not able to save invalid form');
Copy link
Contributor

Choose a reason for hiding this comment

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

$me->wantTo('see that I am not able to save an invalid form');?

public function saveInvalidForm($productId)
{
$this->tester->amOnPage('/admin/product/edit/' . $productId);
// scroll to have the button visible because of the fixed bars
Copy link
Contributor

Choose a reason for hiding this comment

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

is it necessary to scroll down? I thought that when you do just $this->tester->clickByCss('.js-parameters-item-add');, it does not matter that the element is not in viewport, or does it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, there is the problem with fixed elements with higher z-index. Click method will do the scroll to element like function

document.querySelector('.js-parameters-item-add').scrollIntoView()

and then client will try to click on it but there is element fixed bar above it and codeception will fail on error

Element \<a href="#" class="js-parameters-item-add btn btn--plus">...\</a> 
is not clickable at point (338, 15). 
Other element would receive the click: \<header class="web__header">...\</header>

CHANGELOG.md Outdated
@@ -61,6 +61,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- [#260 - Admin: Js validation: dynamically added parameters are now validated](https://github.com/shopsys/shopsys/pull/260)
- validation is evaluated after dynamic parameters are added
- jQuery migration console error logging is no more
- support for multiple option of Flag and Brand items were added for form to be validated and not submitted if it is invalid
Copy link
Contributor

Choose a reason for hiding this comment

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

I honestly do not know what to do with this problem, the implementation in JsFormValidatorFactory::normalizeViewTransformers() is (already was) really confusing and fragile 😞 It should probably be refactored completely but I do not know how... @PetrHeinz could you please help us here? 👼

@vitek-rostislav
Copy link
Contributor

While testing, I noticed yet another bug but it seems that it is already on master so I created a separate issue - #268

@PetrHeinz PetrHeinz force-pushed the bb-admin-product-detail-parameters-js-validation branch from 7db2a8d to 8e2e30f Compare August 2, 2018 06:07
@PetrHeinz
Copy link
Contributor

After discussion with @boris-brtan we've reduced scope of this PR as there are two unrelated issues atm:

  • The dynamically added inputs do not have JS validation. This is what we aim to fix.
  • On product edit page, the form is submitted even if the values are not valid, i.e. JS validation does not prevent the form from submitting. This is related to BG-2136 fixed in 3771de6, we will create a separate issue for this. Boris wanted to fix it in one of the last commits, which recieved most comment during this CR.

Can someone please do a code review of this fix with the reduced scope?

@PetrHeinz PetrHeinz force-pushed the bb-admin-product-detail-parameters-js-validation branch from 8e2e30f to 6b718b4 Compare August 2, 2018 06:23
@PetrHeinz PetrHeinz changed the title Admin: Js validation: dynamically added parameters are now validated JS validation: dynamically added form inputs are now validated Aug 2, 2018
Copy link
Contributor

@vitek-rostislav vitek-rostislav left a comment

Choose a reason for hiding this comment

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

Hi guys, I reviewed and tested the changes and everything now seems ok, you can pass the task to business validation 👍

CHANGELOG.md Outdated
@@ -78,6 +78,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- [#352 - flushes executed in loops are now executed outside of loop](https://github.com/shopsys/shopsys/pull/352)
- [#342 - procedures are now executed only if relevant columns are changed](https://github.com/shopsys/shopsys/pull/342)
- [#362 - guidelines-for-pull-request.md: fixed indentation of lines and code blocks](https://github.com/shopsys/shopsys/pull/362)
- [#260 - JS validation: dynamically added form inputs are now validated](https://github.com/shopsys/shopsys/pull/260)
Copy link
Contributor

Choose a reason for hiding this comment

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

Please correct changelog entry because new version was released.

Boris Brtáň added 3 commits August 3, 2018 09:28
- Shopsys.validation modified so it applies handlers for newly registered content
- code was made more consistent across usages
- avoids issues during parsing, including problems with parsing whitespace characters
@boris-brtan boris-brtan force-pushed the bb-admin-product-detail-parameters-js-validation branch from 6b718b4 to 569002f Compare August 3, 2018 07:28
@boris-brtan boris-brtan merged commit 87c13b9 into master Aug 3, 2018
@boris-brtan boris-brtan deleted the bb-admin-product-detail-parameters-js-validation branch August 3, 2018 07:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants