Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

236
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda