Replies: 7 comments 1 reply
-
As with many features, Shoelace leans on the platform for form control validation. The recommended approach is the Constraint Validation API, which all form controls support. I do understand that's it a fairly common practice to put validation messages underneath form controls. I think constraint validation works the way it does to prevent layout shifts and jarring behaviors, such as this dialog suddenly expanding on submit. That said, if you want to use that pattern, it's probably best to not use constraint validation at all and go with your own solution. You can use the help text slot for error messages and even customize colors if you want. I'd probably add a class like https://codepen.io/claviska/pen/GRBYMVQ?editors=1010 Even though there are only two fields in that example, you can already see a pattern that could be extracted into a utility for all forms. At this time, I don't have plans to add additional validation techniques to the library. Whether that stance changes in the future depends entirely on feedback and demand from users. |
Beta Was this translation helpful? Give feedback.
-
@claviska Thanks for your answer and the nice demo. It surely cost you quite some time always preparing these nice little demos for your answers, but they are always a great help and absolute gold - a special thanks for all those demos of yours 👍 ❤️ Just for information for anyone who's interested: Articles that may be helpful to implement a solution in userland: |
Beta Was this translation helpful? Give feedback.
-
Let's for now ignore the requirement "possibility for error messages in app language instead of browser language". I think, a good solution should be as noninvasive as possible. I also think it's not even necessary to manipulate any form control attributes (like done in the demos of the two latest comments). /*
The sl-input selector will finally be replaced with a selector
for all SL based form controls, of course.
Only used here for simplicity.
*/
sl-input[data-user-invalid]::part(form-control)::after {
display: block;
color: var(--sl-color-danger-700);
content: attr(data-error);
} Then we must make sure that Now all you have to do in userland is using the following method and setting the form attribute function enhanceFormValidation(form: HTMLFormElement): () => void {
const handleInvalid = (ev: Event) => {
if (form.hasAttribute('data-inline-validation')) {
ev.preventDefault();
}
}
form.addEventListener('invalid', handleInvalid, true);
return () => form.removeEventListener('invalid', handleInvalid);
} |
Beta Was this translation helpful? Give feedback.
-
Actually it's much easier than that, due to the fact that you've done an amazing job with that Three steps:
By the end of the day the only thing we are doing here (besides fixing existing issues) is to make the value of |
Beta Was this translation helpful? Give feedback.
-
This was working in my demos (at least it seemed to work 😉), and this is exactly the way things are working with native form controls: form.addEventListener(
'invalid',
(ev: Event) => {
if (form.hasAttribute('data-inline-validation')) {
ev.preventDefault(); // this will prevent HTML 5 validation tooltips
}
},
true
); |
Beta Was this translation helpful? Give feedback.
-
Actually I think it's quite interesting that this feature can be implemented in just six LLOCs in Shoelace core plus a bit of JS/CSS in userland. Basically because these Nevertheless, I personally would prefer a slightly different solution which would need to add the following line to each of Shoelace currently nine form control components: <div part="form-control-validation-message" hidden>${this.validationMessage}</div> added as last child of the following structure: Plus changing the logic of this a little bit. |
Beta Was this translation helpful? Give feedback.
-
After #1181 will have been implemented all this "inline valiation" stuff can be implemented completely in userland. |
Beta Was this translation helpful? Give feedback.
-
@claviska
Please allow me to talk about one last form feature that I personally really miss in SL:
Showing the validation error messages as red texts below the form controls, instead of the usual HTML5 validation error tooltips
Without any doubts, this way of displaying validation errors is very common.
And it would be great, if alternatively the error messages could be in the app language, and not in the browser language (I mean, out of the box, not by using
setCustomValidity
or whatever manually ).See for example how this looks in IBM Carbon Design
![image](https://camo.githubusercontent.com/4b753598f4621fa1579e189abec64716ee1478503e52977d2c5879ba922d02d0/68747470733a2f2f636172626f6e64657369676e73797374656d2e636f6d2f65653335643130633138313335633761383761306435363062613962326237352f666f726d2d76616c69646174696f6e2e676966)
To implement something similar to this I userland is really complex, I guess you basically have to wrap all SL form controls in new components (at least if you want things framework agnostic) and then in the end you have a bunch of components that cannot easily be mixed up with SL core form controls etc.
Please find here two test implementations of something like that (these experimental libraries that are used here are a complete mess, it's not important how this is actually implemented, as it is not done the right way - whatever the "right way" might be 😄).
Maybe you have to use Chrome, I've not tested those demos with Safari. Be aware that the demos are unfortunately quite buggy.
Login form demo:
https://codesandbox.io/s/login-bgtebe?file=/src/index.ts
Form dialog (the logic when to show the error message is buggy, this was better done in that "Login form demo"):
https://codesandbox.io/s/dialogs-erd22k?file=/src/index.ts
If I recall correctly, you have mentioned somewhere something about "inline validation" or whatever (but I cannot find the corresponding issue on Github).
Do you have by any chance any opinons, ideas, tips or whatever on that topic? Thank you very much in advance.
[Edit] Here's a little Shoelace demo: https://shoelace-dgqwh3tf6-font-awesome.vercel.app/getting-started/form-controls?id=using-inline-validation-messages
Beta Was this translation helpful? Give feedback.
All reactions