Next.js Discord

Discord Forum

New Next.js Form Component

Answered
Tomistoma posted this in #help-forum
Open in Discord
TomistomaOP
Hello everyone, when i try to use new next.js Form component and passing action={/page} to it, if i tried to submit with an empty input it is submiting!

i just need to prevent this if the input value !== "", how to do it ?
Answered by B33fb0n3
then add a onSubmit function and validate your input length there. It could look like this:
onSubmit={(e) => {
          const input = (e.target.elements).q;

          if (input.value.trim().length < 1) {
            e.preventDefault();
            // show somthing
          }
        }}


--- Edit
This can be transfered into a good looking client component to show the user specific feedback as Alex mentioned here: https://nextjs-forum.com/post/1318332105226457181#message-1318625056011587714
View full answer

46 Replies

@Tomistoma still the same
can you share the code how you set up your form?
TomistomaOP
@Tomistoma Click to see attachment
how do you submit your form?
TomistomaOP
by pressing Enter
@Tomistoma by pressing Enter
then add a onSubmit function and validate your input length there. It could look like this:
onSubmit={(e) => {
          const input = (e.target.elements).q;

          if (input.value.trim().length < 1) {
            e.preventDefault();
            // show somthing
          }
        }}


--- Edit
This can be transfered into a good looking client component to show the user specific feedback as Alex mentioned here: https://nextjs-forum.com/post/1318332105226457181#message-1318625056011587714
Answer
@Tomistoma isn't there a way to keep it server component ?
yes, you can handle the empty input also serverside. Get the searchparams and validate it:
export default function Page({
  params,
  searchParams,
}: {
  params: Promise<{ slug: string }>
  searchParams: Promise<{ [key: string]: string | string[] | undefined }>
 }) {
  if(searchParams.q.length <= 0)
    return <p>Your input can't be empty</p>
  else
    return <h1>My Page</h1>
}
@Tomistoma isn't there a way to keep it server component ?
Siberian Flycatcher
Add "required" attribute to your input
@Siberian Flycatcher Add "required" attribute to your input
TomistomaOP
It's not the best solution to me
Siberian Flycatcher
Why not?
It wouldn't work either like OP expected
TomistomaOP
i don't need this
@Tomistoma what do you think about the things that we talked about?
TomistomaOP
not the best solution too
@Tomistoma not the best solution too
what kind of solution do you expect?
TomistomaOP
the search params will still be ?q= if i do this
@B33fb0n3 what kind of solution do you expect?
TomistomaOP
i don't even need it to update search params
@Tomistoma i don't even need it to update search params
ok, when you press 'enter' the browser redirects to the entered action. So you need to do something before the action. Do you agree?
TomistomaOP
i need it to ckeck if there is value then update it
else, do not update
it's just search params
doesn't affect on the current page
@Tomistoma browser does not redirect me
it does.

Pressing enter (onKeyup or onKeydown event)
Browser Look for Form action
Browser sets the names of the input as searchparams to the specified action path
@Tomistoma in my case, it doesn't
how do you think the browser redirects you right now?
Siberian Flycatcher
If you want to use the Form component but don't want it to submit when the input is empty, you need to add "required" to the input, as it is a native way of preventing an HTML form from getting submitted.

Otherwise (if you don't want a native validation popup), you might want to create your own form as a client component, check the state of the input in onSubmit, and do router.push() only when the input value is not empty
@B33fb0n3 how do you think the browser redirects you right now?
TomistomaOP
the browser isn't redirecting me in this case
It still the same page /bank
@Tomistoma the browser isn't redirecting me in this case
remove the "required"
@B33fb0n3 remove the "required"
TomistomaOP
that's what happen on pressing enter
@Tomistoma that's what happen on pressing enter
see: redirected.

From: /bank/
To: /banks?q=
but because there isn't a way to keep that, i will go with client component
i hope they will fix it
and not to submit the form if there is no value.
happy to help