Allow updating submission values client-side before being passed to form action #1156
Replies: 2 comments
-
|
This is likely outside of Conform's scope. Conform intercepts the submit event for validation, but the actual submission is usually handled by either the There are a couple of ways to accomplish this depending on your setup. If you're using NextJS, you can wrap the action with a custom function on the const [lastResult, action] = useActionState(myAction, null);
const [form, fields] = useForm({
// ...
});
return (
<form
{...form.props}
action={async (formData) => {
const passwordClaimSignature = await generatePasswordClaimSignature(
formData.get('username'),
formData.get('password'),
);
formData.set('passwordClaimSignature', passwordClaimSignature);
formData.delete('password');
action(formData);
}}
>
{/* ... */}
</form>
);Alternatively, you can use const [form, fields] = useForm({
// ...
async onSubmit(event, { formData }) {
event.preventDefault();
const passwordClaimSignature = await generatePasswordClaimSignature(
formData.get('username'),
formData.get('password'),
);
formData.set('passwordClaimSignature', passwordClaimSignature);
formData.delete('password');
// Submit using your framework's mechanism, e.g.:
// - submit(formData) with useSubmit() in React Router
// - startTransition(() => action(formData)) with useActionState
// - fetch() for a custom endpoint
},
});In both cases, Conform's validation runs first. If validation fails, neither the Maybe we could expand the scope for a tighter integration in the future. But at the moment I think this should be fairly straightforward to handle yourself. |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for the detailed explanation! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I think allowing updates to submission values client-side before being passed to form action could be a useful feature.
My use case involves performing SRP authentication client side before passing the client side generated tokens to the server action for additional processing and storage. The flow looks something like this:
I had experimented with using the
onSubmit()option, but that appeared to be invoked in parallel to the server action rather than sequentially. Here's an example of what I tried:I'm sure someone can come up with a much more elegant api for this kind of thing, but hopefully this gives a general idea of what we're trying to accomplish. I'm also open to other suggestions if there are already recommended approaches for performing this kind of functionality!
Beta Was this translation helpful? Give feedback.
All reactions