File size: 1,286 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import React from 'react';
import ReactDOM from 'react-dom';
import { useForm } from 'react-hook-form';

export default function App() {
  const { register, errors, getValues, handleSubmit } = useForm();

  return (
    <div className="App">
      <h1>getValue - Compare Field Values</h1>
      <form onSubmit={handleSubmit((data) => console.log(data))}>
        <label>New Password: </label>
        <input
          name="password"
          ref={register({ required: 'Password is required!' })}
        />
        {errors.password && (
          <p style={{ color: 'white' }}>{errors.password.message}</p>
        )}

        <label>Confirm Password: </label>
        <input
          name="passwordConfirmation"
          ref={register({
            required: 'Please confirm password!',
            validate: {
              matchesPreviousPassword: (value) => {
                const { password } = getValues();
                return password === value || 'Passwords should match!';
              },
            },
          })}
        />
        {errors.passwordConfirmation && (
          <p style={{ color: 'white' }}>
            {errors.passwordConfirmation.message}
          </p>
        )}

        <button type="submit">Trigger</button>
      </form>
    </div>
  );
}