noahsalvi / svelte-use-form

The most compact reactive form controller (including Validation) that you'll ever see.
MIT License
252 stars 14 forks source link

Custom validation current field ref #55

Closed maxime-carbonneau closed 1 year ago

maxime-carbonneau commented 1 year ago

Hello,

It would be nice to have access to the current field in a custom validation.

function customValidation(field, value, form) {
...
}

or

function customValidation(value, form) {
    const field = this;
}
noahsalvi commented 1 year ago

Hey, I agree with you. It would definitely be a smart addition. However, I'm not set on how we should implement this syntax wise. IMO customValidator(value, field, form) would make the most sense, where field and form would be optional. The problem with this approach would be that it is a breaking change.

I'll look into this when I'm back from my vacation but I'm also open for suggestions.

maxime-carbonneau commented 1 year ago

My suggestion would be, at first, to bind the current field to the customValidation.

customValidation.bind(field)(value, form);

In version 3, you could change the function signature to customValidation(value, field, form) and maybe keep the bind also.

noahsalvi commented 1 year ago

Looks like this is the way to go. Are you interested in implementing this?

noahsalvi commented 1 year ago

I chose to add the control (field) as an additional parameter instead of binding it. Since it would otherwise not be compatible with arrow functions.

It is now available in version 2.9.0 https://github.com/noahsalvi/svelte-use-form/releases/tag/2.9.0