question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support "valid" and "invalid" events in fieldsets

See original GitHub issue

Background: I’m trying to build a multi-page checkout flow (demo)

checkout-flow

In order to submit all inputs together in the last step, all form inputs are written to amp-state and then added to the last form via hidden inputs. For each step, the next button is enabled once all form inputs are valid.

To avoid duplicating validation logic in amp-bind expressions, I’m binding the button’s disabled state to a state variable, e.g.: shippingAddressValid which tracks the form’s validation status using the valid / invalid event:

<form id="shippingAddressForm" 
      method="post" 
      action-xhr="https://dummy.endpoint.as.form.is.never.submitted" 
      custom-validation-reporting="as-you-go" 
      target="_blank" 
      on="valid:AMP.setState({ shippingAddressValid: true });
              invalid:AMP.setState({ shippingAddressValid: false })">
  <input ...>
</form>
<button disabled [disabled]="!shippingAddressValid">...</button>

Problem: I have to wrap all inputs into a form to be able to access the valid/invalid events. However, the form will never be submitted as the inputs are only written to amp-state. This is not very intuitive and might cause unintended side-effects.

Proposed Solution: support validation events for fieldsets.

Fieldsets already support disabling all nested inputs (a feature I need in my sample as well). This makes them a good place to also support validation events. The snippet above could then be simplified to:

<fieldset on="valid:AMP.setState({ shippingAddressValid: true });
                        invalid:AMP.setState({ shippingAddressValid: false })">
  <input ...>
</fieldset>
<button disabled [disabled]="! shippingAddressValid">...</button>

//cc @choumx @ericlindley-g

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
aghassemicommented, Nov 27, 2017

A bit of related discussion here: https://github.com/ampproject/amphtml/issues/11691

tl;dr: Spec-wise, browsers always assume fieldsets are valid even if all of their inputs are invalid. If we end up doing this, we should reconsider doing https://github.com/ampproject/amphtml/issues/11691 as well.

/cc @cvializ

0reactions
sebastianbenzcommented, Mar 22, 2021

I still think that this would be really good to have.

Read more comments on GitHub >

github_iconTop Results From Across the Web

HTMLInputElement: invalid event - Web APIs | MDN
The invalid event fires when a submittable element has been checked for validity and doesn't satisfy its constraints.
Read more >
HTML oninvalid Event Attribute - W3Schools
The oninvalid event occurs when a submittable <input> element is invalid. For example, the input field is invalid if the required attribute is...
Read more >
Error with Field Sets and Objects - Salesforce Developers
As for the proximate problem: I believe the issue is stemming from $objectType not being able to resolve fieldset. Since Field_Set__C is, I'm ......
Read more >
Native form validation — part 2 - Medium
HTML supports many potentially useful input types and attributes. ... As we saw in part 1, fieldset:invalid works in most browsers and kicks ......
Read more >
Working with forms in JavaScript - KnowledgeHut
These controls can be enriched to support accessibility for ... is invalid then checkValidity will raise an event called invalid Event.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found