Keyur Patel
Keyur Patel

Reputation: 95

React-ReduxForm Password Confirmation not working?

this is my validator function which I created using revalidat library. Where Password is my field.

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

I am applying this in Field level validate to my confirmPassword field. but It's not working perfectly it always show me 'Passwords do not match' . I don't Know what I am doing wrong.

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

Anyone had used another Password Confirmation method. Suggest me

Upvotes: 1

Views: 1495

Answers (2)

zigzag
zigzag

Reputation: 321

Because Sinan's solution would not work for me, I'll post my less dynamic solution, field-specific matching solution

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]}
/>

Upvotes: 0

Sinan Baymammadli
Sinan Baymammadli

Reputation: 808

You can use

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

and then

<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")]}
/>

Upvotes: 4

Related Questions