Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[solidjs] Conditional validation #231

Open
vf-unordinary opened this issue Jul 23, 2024 · 6 comments
Open

[solidjs] Conditional validation #231

vf-unordinary opened this issue Jul 23, 2024 · 6 comments
Assignees
Labels
question Further information is requested

Comments

@vf-unordinary
Copy link

Hey all again.

I was wondering if it is possible to specify a validator, like required, in a conditional way, i.e. from a signal.

I've played with the custom validator,

const [validateField, setValidateField] = createSignal(false);
...

<Field name="password" validate={custom((val) => validateField && !val, 'This field is mandatory)}>
     {(field, props) => ...>}
</Field>

But when submitting it does not show any error.

Any suggestion is really appreciated.

@fabian-hiller
Copy link
Owner

I think you forgot to call the signal. You wrote validateField instead of 'validateField()'.

@fabian-hiller fabian-hiller self-assigned this Jul 23, 2024
@fabian-hiller fabian-hiller added the question Further information is requested label Jul 23, 2024
@vf-unordinary
Copy link
Author

Hello again Fabian, thanks for your help. And sorry for being this late.

I've fixed the code as you suggested, yet during submit it does not validate.

I've reproduced here

https://stackblitz.com/edit/solidjs-templates-ofa5sg

Thanks again

@fabian-hiller
Copy link
Owner

fabian-hiller commented Jul 25, 2024

I have investigated this issue. The problem seems to be that every validation is optional by default. This means that our required() validation is always required to make a field required. custom(…) and other validation functions are only triggered when an input is entered.

You are basically hitting an edge case that our system is not compatible with. A workaround might be to use a Valibot schema to validate your form instead.

@vf-unordinary
Copy link
Author

Ok that make sense. Thanks again!

@caymaynard
Copy link

For me, it would also be useful to have a conditional required validation. I'm currently running a validator without using the custom function ( async (value: Forms.Maybe<string>) => (fieldRequired(value) ? 'this field is required' : '') ) because of this.

Could an extra function, something like customRequired be added?

@fabian-hiller
Copy link
Owner

I recommend that you add it yourself, as Valibot currently takes up most of my time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants