Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

229
Views
How to reset other values when one is introduced in React with react-hook-form

Having this validation schema:

const schema = yup.object().shape({
  second: yup.number(),
  minute: yup.number(),
  triggerName: yup.string().required()
});
  • triggerName should be required but when I write something for minute input, the second input should reset and vice versa.

    const { handleSubmit, register, reset } = useForm({ resolver: yupResolver(schema) });

...

<Input inputId='second' type='number' {...register('second')} />
<Input inputId='minute' type='number' {...register('minute')} />
<Input inputId='trigger-name' label='triggerName' {...register('triggerName')} />

Tried to do something like the following but it throws errors:

<Input inputId='second' type='number' {...reset(), register('second')} />
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

import setValue from useForm hook and set an onChange function on the input you can do this for each input and have different behavior for onChange

const {
 handleSubmit, 
 setValue, 
 register, 
 reset 
} = useForm({ resolver: yupResolver(schema) });

const onChangeMyInput = (e) => {
 reset()
 setValue('second', e.target.value)
}


<Input 
 {...register('second')} 
 onChange={onChangeMyInput}
 inputId='second' 
 type='number' 
/>
about 4 years ago · Juan Pablo Isaza Report

0

register returns an object with below structure:

{ 
  onChange: function(){},
  onBlur: function(){},
  ref: function(){}
}

At the moment you are just passing the object which has returned from register function, but you can add some other functionality to your inputs' onChange like below example (here we reset one another field in the form by calling resetField):

const Input = React.forwardRef(({ inputId, ...otherProps }, ref) => {
  return <input id={inputId} ref={ref} {...otherProps} />;
});

const schema = yup.object().shape(
  {
    second: yup
      .number()
      .nullable()
      .transform((value, originalValue) =>
        String(originalValue).trim() === '' ? null : value
      ),
    minute: yup
      .number()
      .nullable()
      .transform((value, originalValue) =>
        String(originalValue).trim() === '' ? null : value
      ),
    triggerName: yup.string().required(),
  }
);

function TestForm() {
  const {
    handleSubmit,
    register,
    formState: { errors },
    resetField,
  } = useForm({
    resolver: yupResolver(schema),
  });

  console.log(errors);

  const onSubmit = (data) => {
    if (!data.minute && !data.second) {
      alert('on of minute or second field must has value');
      return false;
    }
    alert('success');
    console.log(data);
  };

  const { onChange: onSecondChange, ...secodField } = register('second');
  const { onChange: onMinuteChange, ...minuteField } = register('minute');

  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      <Input
          inputId="second"
          type="number"
          {...secodField}
          onChange={(e) => {
            onSecondChange(e);
            resetField('minute');
          }}
        />
      <Input
        inputId="minute"
        type="number"
        {...minuteField}
        onChange={(e) => {
          onMinuteChange(e);
          resetField('second');
        }}
      />
      <Input
        inputId="trigger-name"
        label="triggerName"
        {...register('triggerName')}
      />
      <input type="submit" value="submit" />
    </form>
  );
}

I've validated to minute or second has value in the onSubmit function because I don't know exactly your business about filling both or one of them, but if you want you can handle this validation by yup by using of when method or by defining some custom rule on your schema.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!