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

[BUG] - INPUT WHEN ISVALID #2844

Closed
Fiqry919 opened this issue Apr 22, 2024 · 13 comments · Fixed by #2987
Closed

[BUG] - INPUT WHEN ISVALID #2844

Fiqry919 opened this issue Apr 22, 2024 · 13 comments · Fixed by #2987
Assignees
Labels
📦 Scope : Components Related to the components 🐛 Type: Bug Something isn't working

Comments

@Fiqry919
Copy link

NextUI Version

2.3.5

Describe the bug

im trying with submit form, when input set isvalid false then cant resubmit form

Your Example Website or App

No response

Steps to Reproduce the Bug or Issue

see error

Expected behavior

as a user

Screenshots or Videos

No response

Operating System Version

windows

Browser

Chrome

Copy link

linear bot commented Apr 22, 2024

@wingkwong
Copy link
Member

@Fiqry919 I'm not sure what the issue is here. When it's invalid, you can't submit the form. Can you show some code or even a video for demonstration?

@Fiqry919
Copy link
Author

sure, this a video
https://github.com/nextui-org/nextui/assets/116353477/ebff2d9b-b20c-4c64-b9bc-9017de52c5b5

@Fiqry919
Copy link
Author

@wingkwong
Copy link
Member

@Fiqry919 Ok now it's more clean. However, can you provide a minimal reproducible environment for us to investigate the issue?

@Moriarty47
Copy link

Moriarty47 commented Apr 24, 2024

I'm run into this issue too.

1

{
"dependencies": {
    // ....
    "@nextui-org/react": "2.2.10",
    "framer-motion": "11.0.24",
    "next": "13.5.1",
    "postcss": "8.4.30",
    "react": "18.2.0",
    "react-dom": "18.2.0"
  }
}

2

{
"dependencies": {
    // ....
    "@nextui-org/react": "2.3.5",
    "framer-motion": "11.0.24",
    "next": "13.5.1",
    "postcss": "8.4.30",
    "react": "18.2.0",
    "react-dom": "18.2.0"
  }
}

Here is a minimal env

@Fiqry919
Copy link
Author

thanks

@wingkwong
Copy link
Member

I think the reason is your validation logic resides after submit. Once the input has an invalid state, submit is blocked. In this case, you can change the state by using validate.

@Moriarty47
Copy link

Rewrite it in this way, it worked. Thanks for your help , @wingkwong .

const schema = {
  username: z
    .string()
    .min(3, 'Please input email')
    .max(30, 'Please input correct email')
    .email('Please input correct email'),
  password: z.string().min(6, 'Please input password'),
};

const validator = (key: keyof typeof schema) => (value: string) => {
  const res = schema[key].safeParse(value);
  console.log(res);
  return !res.success && res.error.issues[0].message || null;
};
const onSubmit = (e: FormEvent<HTMLFormElement>) => {
    e.stopPropagation();
    e.preventDefault();
    const formData = new FormData(e.target as HTMLFormElement);
    const value = {
      username: formData.get('username'),
      password: formData.get('password'),
    };
    console.log(value);
    return value;
  };

  return (
    <main className="flex min-h-screen flex-col items-center justify-between p-24">
      <form onSubmit={onSubmit} className="grid gap-4 grid-cols-1 grid-rows-2">
        <Input
          name="username"
          autoComplete="email"
          required
          autoFocus
          label="Email"
          placeholder="Input your email"
          variant="bordered"
          defaultValue=""
          validate={validator('username')}
        />
        <Input
          name="password"
          label="Password"
          placeholder="Input your password"
          type="password"
          variant="bordered"
          defaultValue=""
          validate={validator('password')}
        />
        <div className="inline-flex items-start justify-between self-start gap-4 mb-4">
          <Button color="primary" type="submit">
            Sign in
          </Button>
        </div>
      </form>
    </main>
  );

@Moriarty47
Copy link

BTW, for the original validation logic, just simply add an attribute noValidate (specs here) to the form tag. It still works.

@Fiqry919
Copy link
Author

I think the reason is your validation logic resides after submit. Once the input has an invalid state, submit is blocked. In this case, you can change the state by using validate.

I don't understand why, but if there is a logic problem, it shouldn't work in other versions.
im trying with props validate is still same.
does this not work just for me

@wingkwong
Copy link
Member

Here is a minimal env

@Moriarty47 can you share your previous version of this stackblitz link? the one to reproduce the issue. The above one got overrode.

@Moriarty47
Copy link

Ok. I think it should look something like this,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📦 Scope : Components Related to the components 🐛 Type: Bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants