简体   繁体   English

React - react-final-form 验证

[英]React - react-final-form validation

I have a question regarding react-final form error message when using record-level validation.在使用记录级验证时,我有一个关于 react-final 表单错误消息的问题。 I have the following field present within FormFilterFields component.我在 FormFilterFields 组件中有以下字段。

Field Names:字段名称:

export const fieldNames = {
  password: "field.password"
};

Field:场地:

            <Field name={fieldNames.password}>
                  {({ input, meta }) => {
                    return (
                      <div className={styles.textInputContainer}>
                        <TextInput
                          type={"password"}
                          label={"password"}
                          value={input.value}
                          onChange={(event)=> {
                            input.onChange(event)
                          }}
                          error={meta.error}
                        />
                      </div>
                    );
                  }}
                </Field>

Form:形式:

     <Form
        onSubmit={() => {}}
        initialValues={this.props.formValues}
        decorators={[formFilterFieldsDecorator]}
        validate={values => {
          const valuesObject: any = values
          const validationErrors = { errors: {} };

          if (valuesObject && valuesObject.field.password && valuesObject.field.password.length < 6){
            validationErrors.errors.field.password = "password is too short"; //errors.field.password is undefined here
          }

          return validationErrors;
        }}
        render={({
          handleSubmit,
          submitting,
          pristine,
          values,
          form,
          invalid
        }) => (
          <form onSubmit={handleSubmit}>
            <FormFilterFields
              form={form}
              onSubmit={event => this.props.onHandleSubmit(event, values)}
              onReset={event => this.props.onHandleReset(event, form)}
            />
            <pre>{JSON.stringify(values)}</pre>
          </form>
        )}
      />

So essentially how would you set the error message for a field name like so:所以本质上你将如何为字段名称设置错误消息,如下所示:

"field.password" “字段.密码”

I have looked at some of the examples but no dice.The alternative would be field level validation which is my last resort as part of my solution.我看过一些例子,但没有骰子。替代方法是字段级验证,这是我作为解决方案的一部分的最后手段。

Any help would be much appreciated任何帮助将非常感激

Thanks谢谢

Did you try你试过了吗

validationErrors.errors['field.password'] = "password is too short";

Edit Try this编辑试试这个

if (!values.field || !values.field.password) {
      errors.field = {password: 'Required'};
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM