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

Server side validation on fields #514

Closed
gerhardcit opened this issue Nov 22, 2024 · 3 comments
Closed

Server side validation on fields #514

gerhardcit opened this issue Nov 22, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@gerhardcit
Copy link

Is your feature request related to a problem? Please describe.
When filling in values in a form, I need to validate fields by doing some remote fetch on the server to see if it's valid.

Describe the solution you'd like
How do I set errors on the server side for a field and send it back to the client?

Describe alternatives you've considered
Tried something like this

const adminValue = form.data.admin;
const valid = await validateAdmin(adminValue);
if (!valid) {
    form.errors.admin = ['Invalid admin version'];
    return fail(400, form);
}

Additional context
Often form input would require some lookups and double checking before we can accept the form.
Not sure I'm missing something, but the docs does not seem to be clear on that?

@gerhardcit gerhardcit added the enhancement New feature or request label Nov 22, 2024
@gerhardcit
Copy link
Author

Documentation shows this

if (form.data.email.includes('spam')) {
      // Will also return fail, since status is >= 400
      // form.valid will also be set to false.
      return message(form, 'No spam please', {
        status: 403
      });
    }

should it not be able to attach the "No spam please" message to the email errors ?

@gerhardcit
Copy link
Author

ok, found it..

if (!valid) {
    form.errors.admin = ['Invalid admin version'];
    return fail(400, {form});
}

duh...

@ciscoheat
Copy link
Owner

setError is also a convenient way of setting form errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants