0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

バリデーション(validate)かけたuseFormでvalueで値表示できない (mantineだけど通常のuseFormも同じかな?)

Last updated at Posted at 2022-08-23

結論: form.setValuesを使う

const editUserToForm = (recoilEditUser: RegisteredUsers) => {
    form.setValues({
      id: "5",
      name: "へのへのもへじ",
      age: "39",
      todo: "味噌がもうないから買いに行く",
      email: "todo@email.com",
    });
  };

上記に対してのuseForm


import { useForm } from "@mantine/form";

export const useUserForm = () => {
  const form = useForm({
    initialValues: {
      id: "",
      name: "",
      age: "",
      todo: "",
      email: "",
     
    },

    validate: {
      name: (name_value) =>
        name_value.length < 1 ? "名前は必須入力です" : null,
      age: (age_value) =>
        age_value.length == 0
          ? null
          : /(^\d?\d{1}$)|(^1[0-4]{1}\d{1}$)|(^150$)/.test(age_value)
          ? null
          : "年齢は数字で150以下で入力してください",
      todo: (todo_value) =>
        todo_value.length < 1 ? "todoは必須入力です" : null,
      email: (mail_value) =>
        mail_value.length === 0
          ? null
          : /^\S+@\S+$/.test(mail_value)
          ? null
          : "メールアドレス形式で入力してください",
    },
  });

  return form;
};

このuseFormのform

 <form onSubmit={getFormUser}>
          <TextInput
            disabled
            label="id"
            placeholder="id"
            {...form.getInputProps("id")}
          />
          <TextInput
            label="名前"
            placeholder="Name"
            {...form.getInputProps("name")}
          />
          <TextInput
            mt="md"
            label="年齢"
            placeholder="年齢を入力してください"
            {...form.getInputProps("age")}
          />
          <TextInput
            mt="md"
            label="todo"
            placeholder="適当な文字"
            {...form.getInputProps("todo")}
          />
          <TextInput
            mt="md"
            label="Email"
            placeholder="your@email.com"
            {...form.getInputProps("email")}
          />
          <Button className="bg-black" type="submit">
            Submit
          </Button>
        </form>

反映されないパターン

バリデーションかけたuseFormだと「へのへのもへじ」は表示されない

         <TextInput
            label="名前"
            value="へのへのもへじ"
            {...form.getInputProps("name")}
          />

反映されるパターン

バリデーションかけていないもの?...formを使っていなければvalueで動く

          <TextInput
            value="へのへのもへじ"
            onChange={(event) => setValue(event.currentTarget.value)}
          />

・・・とりあえず実装重視だったので原因追求はあとです

参考
ReactUIライブラリMantineのフォームAPIを使用する

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?