-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Mantine Form Restricts Server Action Support With Client & Server Validation #7142
Comments
Hi @anthonyalayo, it looks like your problem might be resolved if you bind the submission action to a button, rather than As your LoginForm is a client component, it would make more sense to submit via the button onClick. I believe in Nextjs, it's possible to attach a server action to the client button component. You can await the submission and get back a json object. Within the server action, you can perform server side validation as well. |
@chewhx have you been successful with that approach? Do you have an example? |
Will this work for you @anthonyalayo? I've added const onSubmit: OnSubmit<Values, TransformValues> =
(handleSubmit, handleValidationFailure) => (event) => {
if (onSubmitPreventDefault === 'always') {
event?.preventDefault();
}
const results = validate();
if (results.hasErrors) {
if (onSubmitPreventDefault === 'validation-failed') {
event?.preventDefault();
}
handleValidationFailure?.(results.errors, $values.refValues.current, event);
} else {
handleSubmit?.(transformValues($values.refValues.current) as any, event);
}
}; |
For 2: I do not think that it is a good idea to set name attribute with |
This works, thanks @rtivital ! |
@rtivital , when is the next release planned? |
Next week |
Fixed in 7.14.2 |
Dependencies check up
What version of @mantine/* packages do you have in package.json?
7.13.5
What package has an issue?
@mantine/form
What framework do you use?
Next.js
In which browsers you can reproduce the issue?
All
Describe the bug
When attempting to perform both client side and server side validation with
@mantine/form
and server actions, the current API disallows this from working. Without modifications, you cannot callevent?.currentTarget.requestSubmit()
from the callback ofform.onSubmit()
after placing an action returned fromuseActionState
in theaction
prop as required.For comparison, this is how conform allows form submission with both client side and server side validation:
https://conform.guide/integration/nextjs
With the relevant snippet of code here:
With this configuration, server actions works seamlessly.
If possible, include a link to a codesandbox with a minimal reproduction
No response
Possible fix
The API can be updated to work correctly with server actions via
useActionState
. For context, here is a quick Q&A with the author of conform: edmundhung/conform#818In order for it to work well with
@mantine/form
, the following changes would be required:https://github.com/mantinedev/mantine/blob/master/packages/%40mantine/form/src/use-form.ts#L205
If we move that line into the if statement for no errors below, it won't run on success by default. Searching up default javascript behavior, I believe if we return true/false from the form.onSubmit(), that should control whether the action is run.
name
property. Forms through server action require this (since they are being handled by html):We should update the
{...form.getInputProps('email')}
call to also include the name attribute.This works, but it would be nice to have
form.onSubmit()
do this by default (like conform), since that will be a standard use case. That would finally reduce the usage to looking like the following:Which is clean and intuitive.
Points 2 and 3 are nice to have for developer experience, but point 1 is a necessity. There's no way in javascript to reverse the effect of
event.preventDefault()
.Self-service
The text was updated successfully, but these errors were encountered: