Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

105
Vistas
How to set a numeric value in defaultValues for react-hook-form in order to be required?

There is a form that contains two inputs, a string and a number both being required.

Here is the schema:

const schema = yup.object().shape({
  name: yup.string().required(),
  age: yup.number().required()
});

Afterwards, it is react-hook-form:

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

As you can see, there is defaultValues which has emptyObject.

Here it is:

const emptyObject = {
  name: '',
  age: 0
}

The name part works fine, I cannot submit the data if there is no input written in that textbox but for age it doesn't require any value. I know it checks for value and it finds it is 0 and it considers there is an input.

But how to change it to make it work? Tried with null but still doesn't work.

I have to mention that it is React with Typescript so there is also an interface which throws error when age is set to null.

  MyData: {
    name: string;
    age: number;
  }
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Make the age an empty string as well, just for your needs and when its time to submit the form just add some functionality to convert the string to number if its critical for your app.

From the DOCS, you can not set it to any undefined value:
It is encouraged that you set a defaultValue for all inputs to non-undefined such as the empty string or null.

Link to DOCS

about 4 years ago · Juan Pablo Isaza Denunciar

0

I got this from https://react-hook-form.com/get-started#Applyvalidation

import React from "react";
import { useForm, SubmitHandler } from "react-hook-form";

interface IFormInput {
  firstName: string;
  lastName: string;
  age: number;
}

export default function App() {
  const { register, handleSubmit } = useForm<IFormInput>();
  const onSubmit: SubmitHandler<IFormInput> = data => console.log(data);
   
  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      <input {...register("firstName", { required: true, maxLength: 20, placeHolder: "John" })} />
      <input {...register("lastName", { pattern: /^[A-Za-z]+$/i })} />
      <input type="number" {...register("age", { min: 18, max: 99 })} />
      <input type="submit" />
    </form>
  );
}

can this be an example to go on with?

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda