html icon indicating copy to clipboard operation
html copied to clipboard

setCustomValidity() seems pointless on <fieldset>

Open domenic opened this issue 3 years ago • 5 comments

Constraint validation only checks submittable elements. So if you do fieldset.setCustomValidity("Something is wrong!") this has no impact on preventing form submission.

I think constraint validation should also check fieldsets?

There might be other elements with the constraint validation API added to them but outside the submittable elements list...

domenic avatar Jul 16 '21 22:07 domenic

The idea was that because it's a Listed element it should have the same general-purpose APIs as the other Listed elements: https://lists.whatwg.org/pipermail/whatwg-whatwg.org/2009-August/064766.html. I'm not sure how much sense this makes though.

I'm also not sure how much sense it would make for constraint validation to check fieldset elements. They are not form controls after all.

annevk avatar Jul 19 '21 13:07 annevk

I'm also not sure how much sense it would make for constraint validation to check fieldset elements. They are not form controls after all.

https://jsbin.com/nucekozegi/1/edit?html,output (see https://github.com/whatwg/html/issues/6868 for motivation) is an example where I think it would make sense. I.e. saying "something in how you've input into this fieldset is invalid", when it might not be possible to pinpoint specific elements.

domenic avatar Jul 19 '21 16:07 domenic

FWIW, we often have forms where one of two fields is required (e.g., first or last name, phone or email, etc.). Since a <fieldset> is the right semantics for grouping fields, it seems like it would be appropriate to set the custom validity at the fieldset level—and be able to use myFieldset.validity and myFieldset.validationMessage for displaying feedback to the user.

robertwbradford avatar Feb 15 '24 16:02 robertwbradford

Fieldset supports :invalid and other Constraint Validation stuff, so it stands to reason a developer should be able to setCustomValidity().

Addresses are a good example where a combination of address fields may satisfy the application's requirements. Independent validation of individual fields in this use case is not really helpful because their validity depends on what else has been done in other fields:

  • address line 1 (required if line 2 is empty)
  • address line 2 (required if line 1 is empty)
  • city (required if zip code is empty)
  • state (required if zip code is empty)
  • zip code (required if city or state are empty)

The application could run that custom validation logic on submit and set an appropriate message based on what it finds using fieldset.setCustomValidity(message), which triggers :invalid on the fieldset and other Constraint Validation features.

jfbrennan avatar Apr 24 '24 14:04 jfbrennan