Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Call validation before submitting the order #4561

Merged
merged 4 commits into from
Aug 16, 2021
Merged

Conversation

senadir
Copy link
Member

@senadir senadir commented Aug 9, 2021

This PR fixes the autofill issue, see #4495 for what's happening.
TL;DR, we only run validation on mount and blur, autoFill doesn't trigger blur, meaning we didn't run any validation. Additionally, all inputs mount with an error if empty, meaning when we submit an order, errors aren't cleared.

This PR runs validation on isBeforeProcessing.

Fixes #4495

How to test the changes in this Pull Request:

  1. On a private window, autoFill the form.
  2. Submit the order, it should pass fine.
  3. Again, on a private window, autoFill the order.
  4. Mess up the email field or postcode with an invalid value.
  5. Try submitting, make sure it shows the error.
  6. Fix the field, make sure there is no jumping around.
  7. Submit again, it should pass.

Changelog

Fix autofill triggering validation errors for valid values in Checkout block

@senadir senadir added type: bug The issue/PR concerns a confirmed bug. block: checkout Issues related to the checkout block. labels Aug 9, 2021
@senadir senadir added this to the 5.7.0 milestone Aug 9, 2021
@senadir senadir self-assigned this Aug 9, 2021
@senadir senadir requested a review from a team as a code owner August 9, 2021 13:44
@senadir senadir requested review from ralucaStan and removed request for a team August 9, 2021 13:44
@github-actions
Copy link
Contributor

github-actions bot commented Aug 9, 2021

Size Change: +30.2 kB (+3%)

Total Size: 1.13 MB

Filename Size Change
build/active-filters-frontend.js 8.25 kB -6 B (0%)
build/active-filters.js 7.83 kB +8 B (0%)
build/all-products-frontend.js 23.1 kB +204 B (+1%)
build/all-products.js 37.1 kB +45 B (0%)
build/all-reviews.js 9.6 kB +26 B (0%)
build/atomic-block-components/add-to-cart--atomic-block-components/button--atomic-block-components/image---a7e2bb9b.js 2.66 kB +94 B (+4%)
build/atomic-block-components/add-to-cart--atomic-block-components/button.js 1.81 kB -8 B (0%)
build/atomic-block-components/add-to-cart-frontend.js 8.39 kB +6 B (0%)
build/atomic-block-components/add-to-cart.js 7.74 kB +13 B (0%)
build/atomic-block-components/button-frontend.js 1.74 kB -3 B (0%)
build/atomic-block-components/button.js 875 B -1 B (0%)
build/atomic-block-components/category-list-frontend.js 468 B -2 B (0%)
build/atomic-block-components/image-frontend.js 1.88 kB -2 B (0%)
build/atomic-block-components/image.js 1.34 kB -2 B (0%)
build/atomic-block-components/price.js 2.11 kB -1 B (0%)
build/atomic-block-components/rating.js 566 B -1 B (0%)
build/atomic-block-components/sale-badge-frontend.js 859 B -1 B (0%)
build/atomic-block-components/sale-badge.js 867 B -1 B (0%)
build/atomic-block-components/sku-frontend.js 388 B -1 B (0%)
build/atomic-block-components/stock-indicator-frontend.js 610 B -2 B (0%)
build/atomic-block-components/stock-indicator.js 611 B -1 B (0%)
build/atomic-block-components/summary-frontend.js 905 B +1 B (0%)
build/atomic-block-components/summary.js 912 B +3 B (0%)
build/atomic-block-components/tag-list-frontend.js 466 B -2 B (0%)
build/atomic-block-components/tag-list.js 471 B -1 B (0%)
build/atomic-block-components/title-frontend.js 1.43 kB -3 B (0%)
build/atomic-block-components/title.js 1.28 kB -4 B (0%)
build/attribute-filter-frontend.js 18.1 kB +51 B (0%)
build/attribute-filter.js 11.9 kB +3 B (0%)
build/blocks-checkout.js 21.2 kB +6 B (0%)
build/cart-frontend.js 90.6 kB +13.4 kB (+17%) ⚠️
build/cart.js 45.8 kB +217 B (0%)
build/checkout-blocks/sample.js 173 B -1 B (-1%)
build/checkout-frontend.js 94.7 kB +13.4 kB (+16%) ⚠️
build/checkout-i2-frontend.js 51.6 kB +248 B (0%)
build/checkout-i2.js 48.7 kB +293 B (+1%)
build/checkout.js 48.8 kB +237 B (0%)
build/featured-category.js 7.81 kB +16 B (0%)
build/featured-product.js 9.51 kB +25 B (0%)
build/handpicked-products.js 6.35 kB +10 B (0%)
build/price-filter-frontend.js 14.4 kB +146 B (+1%)
build/price-filter.js 9.56 kB -21 B (0%)
build/product-best-sellers.js 6.69 kB +6 B (0%)
build/product-categories.js 3.38 kB -1 B (0%)
build/product-category.js 7.56 kB +3 B (0%)
build/product-new.js 6.85 kB +5 B (0%)
build/product-on-sale.js 7.21 kB +11 B (0%)
build/product-search.js 2.66 kB -10 B (0%)
build/product-tag.js 6.67 kB +7 B (0%)
build/product-top-rated.js 6.82 kB +7 B (0%)
build/products-by-attribute.js 7.79 kB +7 B (0%)
build/reviews-by-category.js 11.6 kB +21 B (0%)
build/reviews-by-product.js 13.1 kB +13 B (0%)
build/reviews-frontend.js 9.01 kB +2 B (0%)
build/single-product-frontend.js 26 kB +197 B (+1%)
build/single-product.js 9.8 kB +19 B (0%)
build/vendors--atomic-block-components/add-to-cart-frontend.js 20.2 kB +167 B (+1%)
build/vendors--atomic-block-components/price-frontend.js 5.71 kB +1 B (0%)
build/wc-blocks-data.js 11 kB +155 B (+1%)
build/wc-blocks-editor-style-rtl.css 15.3 kB -3 B (0%)
build/wc-blocks-editor-style.css 15.4 kB -3 B (0%)
build/wc-blocks-style-rtl.css 20 kB +133 B (+1%)
build/wc-blocks-style.css 20 kB +132 B (+1%)
build/wc-blocks-vendors-style-rtl.css 1.37 kB +319 B (+30%) 🚨
build/wc-blocks-vendors-style.css 1.37 kB +319 B (+30%) 🚨
build/wc-blocks-vendors.js 253 kB +275 B (0%)
ℹ️ View Unchanged
Filename Size
build/atomic-block-components/add-to-cart--atomic-block-components/image--atomic-block-components/title.js 334 B
build/atomic-block-components/category-list.js 476 B
build/atomic-block-components/price-frontend.js 2.1 kB
build/atomic-block-components/rating-frontend.js 561 B
build/atomic-block-components/sku.js 394 B
build/price-format.js 1.37 kB
build/wc-blocks-google-analytics.js 1.98 kB
build/wc-blocks-middleware.js 1.47 kB
build/wc-blocks-registry.js 2.74 kB
build/wc-blocks-shared-context.js 1.54 kB
build/wc-blocks-shared-hocs.js 1.75 kB
build/wc-blocks.js 3.5 kB
build/wc-payment-method-bacs.js 806 B
build/wc-payment-method-cheque.js 806 B
build/wc-payment-method-cod.js 898 B
build/wc-payment-method-paypal.js 839 B
build/wc-payment-method-stripe.js 12.2 kB
build/wc-settings.js 2.91 kB

compressed-size-action

@senadir
Copy link
Member Author

senadir commented Aug 9, 2021

@nerrad what are your concerns on having useCheckoutContext being used in the input field? It would cause issues if we used this input outside Cart and Checkout.

@ralucaStan
Copy link
Contributor

@nerrad what are your concerns on having useCheckoutContext being used in the input field? It would cause issues if we used this input outside Cart and Checkout.

I don't think we should have business logic inside the input component, especially if we are looking at exporting them at some point. Because there is no event we could catch on autofill, I'm thinking we could we have something like a forceValidation prop, which reruns validation once the prop value is true?

@nerrad
Copy link
Contributor

nerrad commented Aug 9, 2021

I don't think we should have business logic inside the input component, especially if we are looking at exporting them at some point. Because there is no event we could catch on autofill, I'm thinking we could we have something like a forceValidation prop, which reruns validation once the prop value is true?

I agree with Raluca here. If there's need to force revalidation via some logic, it makes sense to abstract the logic outside of the input and use props to trigger that re-validation.

@senadir
Copy link
Member Author

senadir commented Aug 10, 2021

I agree that we should have some sort of external trigger that calls validation, but having a prop that forces validation doesn't make sense to me.
This prop feels ephemeral to me and doesn't make sense as a prop.
Being a declarative UI, how should a component like this behave? <ValidatedInput value={value} onChange={onChange} forceRevalidate={ true } />
Should this keep revalidating on each mount, should it revalidate on mount, on what level? forceRevalidate isn't an event to react to, and isn't a static prop, it feels very imperative thing.

I might be missing something, but do we have other examples or precedents of using such pattern?

IMHO, we should probably be rethinking of how we do validation to make it more accessible. We store validation errors outside components yet can decide if we can delete them inside the component, which is already a lot of coupling. Libraries like formik have the validation schema (function) that is accessible from the from level. This allows you to run validation inside the component reacting to certain events like onChange or onBlur, and also outside the component on the form level on demand or at certain lifecycle events.

Such workaround should be currently acceptable until we find a better refactor. Having an extra forceValidation would only cause us to go even deeper into the validation issue.

I did attempt to add that prop to the component and it caused significant prop drilling since inputs are really deep into the tree.

@ralucaStan
Copy link
Contributor

@senadir if we could move the logic at the form level it would be great.

I agree that the input should just receive info about the state it should render (error, valid, etc) and the form is the place where the state should be available.

@mikejolley mikejolley added the status: blocker Used on issues or pulls that block work from being released. label Aug 16, 2021
@github-actions
Copy link
Contributor

Remove extra validation call after refactoring the valida...

Remove extra validation call after refactoring the validation system.


https://github.com/woocommerce/woocommerce-gutenberg-products-block/blob/17cd6699b9cc780e7c50b136704627836901091d/assets/js/base/components/text-input/validated-text-input.tsx#L121-L130

🚀 This comment was generated by the automations bot based on a todo comment in 17cd669 in #4561. cc @senadir

Copy link
Member

@mikejolley mikejolley left a comment

Choose a reason for hiding this comment

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

I tested this using an invalid email address and just autofill and both cases worked as described. Using Edge in private mode.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
block: checkout Issues related to the checkout block. status: blocker Used on issues or pulls that block work from being released. type: bug The issue/PR concerns a confirmed bug.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Form autofill doesn't work for Checkout block.
4 participants