Cross field validation and customized feedback visibility based on interaction states and types #1159
Replies: 1 comment
-
Nice thoughts and suggestions. Cross Field validationWould be very cool to add as an example to the docs. An alternative in some cases can be to put the validation on the wrapping fieldset. Would it also be possible to change the Maybe we can do (pseudo code): _showFeedbackConditionFor(type) {
const originalCondition = (type) => (this.touched && this.dirty) || this.prefilled || this.submitted;
if (typeof this.showFeedbackConditionFor === 'function') {
return this.showFeedbackConditionFor(type, originalCondition)
}
return originalCondition(type);
} <first-input .showFeedbackConditionFor=$"(type, originalCondition) => originalCondition(type) && second.showsFeedbackFor.includes(type)"> So we would not have to set prefilled (this feels a bit hacky). Focus first error showing field
Password manager supportWould go for 3 (lack of browser and plugin support for other opts) Validator non-static getMessagei'm not sure if I understand completely. |
Beta Was this translation helpful? Give feedback.
-
Hi.
In a pet project I decided I needed an authentication form and I used Lion for it mostly because validators are so easy to create and it has all the behavior of showing/not showing feedback based on interaction states already working.
The form ended up pretty cool and it works very nicely, here's a small gif. Yes, it's both a form for logging in and signing up, my auth form component transforms the form on demand.
https://i.imgur.com/uVRhngW.gif
I ran into a few things that I implemented, but that I think could be improved on lion's end.
Cross field validation
The example here is the password match validator. This was quite tricky to get right, because of a few requirements:
touched + dirty || submitted || prefilled
How I ended up implementing:
As you can see, we pass both the first and second field as params so we can easily access them when evaluating validity.
The event listener in the constructor is what ensures that both fields are kept in sync. It's perhaps not the cleanest solution but it works well.
The reason I implemented it like this is because:
It's admittedly a little hacky and weird and that's because my password match validator is pretty "dumb" and is not really aware of the interaction states of both fields, and changing the show/not show feedback condition based on it.
Suggestion
What would be really cool is if we had some way in Lion to create Validators that accept multiple fields, so that:
Focus first error showing field
It's not really clear from the GIF perhaps, but my form is a bit smart about shifting focus whenever the user submits using either the login or signup buttons. If there's a problem, it will focus the first field that has an error, so the user can immediately start fixing it. This even applies to fields that have an error but feedback is not showing. For example, I fill in a username + password and click "signup". The first field with an error is confirm password field, so focus goes to that. The feedback is not shown however because the field is not yet touched + dirty || submitted || prefilled.
It would be quite cool if we could make this a default feature in lion. Perhaps as opt-in, perhaps as smart default with an opt-out. I propose the latter, because I think it fits the overwhelming majority use case.
My form does it like so:
Password manager support
My form is working with password managers. The reason is, the native input fields don't have any shadow boundary above them. However, we should be able to make password managers work with input fields inside shadow roots.
How?
querySelector
todeepQuerySelector
. https://www.npmjs.com/package/query-selector-shadow-dom@ruphin I'm tagging you because I heard you had some kind of experiment for password managers, my assumption would be it will be most similar to suggestion 3? Or do you have another way?
Validator non-static getMessage
Quite often you will want your validation feedback message to depend on your validation params (e.g.
{ min: 4, max: 7 }
).Currently your first attempt will be getMessage which static, and params is only available on the instance. We already have a demo explaining that you can instantiate a Validator and override the getMessage config option "publicly", but we don't have a demo yet that shows you can extend a validator and override the protected _getMessage. Probably someone will force themselves into having localized validation messages since it works with the static method and in translations the params are always passed, before they realize there's a protected way which does not require you to use localized approach.
We should probably also add a demo of a localized validation message example, I had to go into source code to figure out how that works exactly.
Beta Was this translation helpful? Give feedback.
All reactions