反应-Redux表单密码确认不起作用?



这是我使用 revalidat 库创建的验证器函数。其中密码是我的字段。

const validatePass = matchesField('password')({
message: 'Passwords do not match',
});

我正在字段级别验证中将其应用于我的确认密码字段。 但它不能完美运行,它总是向我显示"密码不匹配"。我不知道我做错了什么。

<Field
name="confirmPassword" component={RenderTextField} type="password"
placeholder="Confirm Password" width="large" status="dafault" title="Confirm Password"
validate={[requireField,validatePass]}
/>

任何人都使用了另一种密码确认方法。建议我

您可以使用

export const match = matchName => (value, allValues, props) =>
value !== allValues[matchName]
? `This field must match with ${matchName} field`
: undefined;

然后

<Field
type="password"
name="password"
label="Password"
component={FormField}
validate={[required, minLength(6)]}
/>
<Field
type="password"
name="confirmPassword"
label="Confirm Password"
component={FormField}
validate={[required, match("password")]}
/>

因为 Sinan 的解决方案对我不起作用,所以我会发布我的动态性较差的解决方案,特定于字段的匹配解决方案

export const matchPassword = (value, allValues) =>
value !== allValues.password
? `This field must match with your password field`
: undefined;
<Field
type="password"
name="password"
label="Password"
component={FormField}
validate={[required, minLength(6)]}
/>
<Field
type="password"
name="confirmPassword"
label="Confirm Password"
component={FormField}
validate={[required, matchPassword]}
/>

最新更新