Formik验证在修改后没有显示。

huangapple go评论89阅读模式
英文:

Formik validation is not showing after the modification

问题

我对 Formik 还不太了解。我搜索了几次,但没有找到可行的解决方案。
所以我需要您的帮助来解决这个问题。

我的 Formik 表单如下所示,

<Formik initialValues={initialValues} onSubmit={onSubmit}>
  <Form>
    <div>
      <div>
        <div>
          <Field
            type="text"
            id="firstName"
            name="firstName"
            className="form-control"
            placeholder="名字"
            validate={ValidateName}
          />
          <ErrorMessage name="firstName">
            {msg => <div style={errorMsg}>{msg}</div>}
          </ErrorMessage>
        </div>
      </div>
    </div>
  </Form>
</Formik>

之后,我添加了以下修改,

const searchParams = new URLSearchParams(document.location.search);

const [firstName, setFirstName] = useState(searchParams.get("firstName") || "");

<div className="col-md-6 col-sm-12">
  <div className="form-group">
    <Field
      type="text"
      id="firstName"
      name="firstName"
      className="form-control"
      placeholder="名字"
      onChange={(e) => setFirstName(e.target.value)}
      validate={ValidateName}
      value={firstName}
    />

    <ErrorMessage name="firstName">
      {msg => <div style={errorMsg}>{msg}</div>}
    </ErrorMessage>
  </div>
</div>

所有都正常工作,但验证消息未显示在表单中。ValidateName 返回错误消息。

请告诉我我在哪里出错了。谢谢!

英文:

I'm new to formik. i searched couple of times and didn't find a working solution.
so i need your help to resolve this issue.

my formik form as below,

          &lt;Form&gt;
            &lt;div&gt;
              &lt;div&gt;
                &lt;div&gt;
                  &lt;Field
                    type=&quot;text&quot;
                    id=&quot;firstName&quot;
                    name=&quot;firstName&quot;
                    className=&quot;form-control&quot;
                    placeholder=&quot;First Name&quot;
                    validate={ValidateName}
                  /&gt;
                  &lt;ErrorMessage name=&quot;firstName&quot;&gt;
                    {msg =&gt; &lt;div style={errorMsg}&gt;{msg}&lt;/div&gt;}
                  &lt;/ErrorMessage&gt;
                &lt;/div&gt;
              &lt;/div&gt;

after that i added some modification as below,

const searchParams = new URLSearchParams(document.location.search);

const [firstName, setFirstName] = useState(searchParams.get(&quot;firstName&quot;) || &quot;&quot;);

&lt;div className=&quot;col-md-6 col-sm-12&quot;&gt;
    &lt;div className=&quot;form-group&quot;&gt;
         &lt;Field
            type=&quot;text&quot;
            id=&quot;firstName&quot;
            name=&quot;firstName&quot;
            className=&quot;form-control&quot;
            placeholder=&quot;First Name&quot;
            onChange={(e) =&gt; setFirstName(e.target.value)}
            validate={ValidateName}
            value={firstName}
         /&gt;

          &lt;ErrorMessage name=&quot;firstName&quot;&gt;
               {msg =&gt; &lt;div style={errorMsg}&gt;{msg}&lt;/div&gt;}
          &lt;/ErrorMessage&gt;
        &lt;/div&gt;
     &lt;/div&gt;

all are working fine, but validation message is not showing in the form. ValidateName returning error message

please let me know where i did the mistake. Thanks

答案1

得分: 1

这里以下的部分:

{msg =&gt; &lt;div style={errorMsg}&gt;{msg}&lt;/div&gt;}

应该改成如下:

{msg &amp;&amp; &lt;div style={errorMsg}&gt;{msg}&lt;/div&gt;}
英文:

This here below.

{msg =&gt; &lt;div style={errorMsg}&gt;{msg}&lt;/div&gt;}

should be as below.

{msg &amp;&amp; &lt;div style={errorMsg}&gt;{msg}&lt;/div&gt;}

答案2

得分: 0

已排序。当我清除输入并单击输入框外部时,显示了错误消息。感谢所有关注我的问题的人。

英文:

Sorted. When I cleared the input and clicked outside the input, an error message was shown. Thank you to everyone who looked into my problem.

huangapple
  • 本文由 发表于 2023年6月26日 17:19:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76555291.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定