Next.js Discord

Discord Forum

New Next.js Form Component

Answered
Tomistoma posted this in #help-forum
Open in Discord
Avatar
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

Avatar
you can add a minlength attribute to your input element. Keep in mind, that this is just a browser validation
Avatar
TomistomaOP
still the same
Image
Avatar
can you share the code how you set up your form?
Avatar
TomistomaOP
Image
Avatar
how do you submit your form?
Avatar
TomistomaOP
by pressing Enter
Avatar
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
Avatar
TomistomaOP
isn't there a way to keep it server component ?
Avatar
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>
}
Avatar
Siberian Flycatcher
Add "required" attribute to your input
Avatar
TomistomaOP
It's not the best solution to me
Avatar
Siberian Flycatcher
Why not?
Avatar
It wouldn't work either like OP expected
Avatar
TomistomaOP
Image
i don't need this
Avatar
@Tomistoma what do you think about the things that we talked about?
Avatar
TomistomaOP
not the best solution too
Avatar
what kind of solution do you expect?
Avatar
TomistomaOP
the search params will still be ?q= if i do this
Image
Avatar
that's correct
Avatar
TomistomaOP
i don't even need it to update search params
Avatar
ok, when you press 'enter' the browser redirects to the entered action. So you need to do something before the action. Do you agree?
Avatar
TomistomaOP
i need it to ckeck if there is value then update it
else, do not update
browser does not redirect me
it's just search params
doesn't affect on the current page
Avatar
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
Avatar
TomistomaOP
in my case, it doesn't
it still the samd page /bank
Avatar
how do you think the browser redirects you right now?
Avatar
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
Avatar
TomistomaOP
the browser isn't redirecting me in this case
It still the same page /bank
Avatar
remove the "required"
Avatar
TomistomaOP
Yeah i will go with that
Image
that's what happen on pressing enter
see: redirected.

From: /bank/
To: /banks?q=
Avatar
TomistomaOP
I thought there is a possible way to keep my component on server side
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.
Avatar
happy to help