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

243
Vistas
Formik - Trigger validation from very first keypress

On the following StackBlitz I have a very simple form validation. The validation is done when the user clicks the submit button.

https://stackblitz.com/edit/react-formik-yup-example-uhdg-83teyn?file=Registration.js

import { ErrorMessage, Field, Form, Formik } from 'formik';
import React from 'react';
import * as Yup from 'yup';

export default () => {
  return (
    <Formik
      initialValues={{
        email: '',
      }}
      validationSchema={Yup.object().shape({
        email: Yup.string()
          .required('Email is required.')
          .email('Email is invalid.'),
      })}
      onSubmit={(values, { setSubmitting }) => {
        console.log(values);
        setSubmitting(false);
      }}
      enableReinitialize
    >
      {({ setFieldValue }) => (
        <Form>
          <div>
            <Field
              type="text"
              name="email"
              onChange={({ target: { value } }) => {
                console.log(value);
                setFieldValue('email', value);
              }}
            />
            <ErrorMessage name="email">
              {(error) => <div style={{ color: '#f00' }}>{error}</div>}
            </ErrorMessage>
          </div>
          <input type="submit" value="Submit" />
        </Form>
      )}
    </Formik>
  );
};

What I need is: Trigger the validation from the very first keypress (please notice that I'm not interested in validate it when the component is mounted, but just after the first keypress).

In other words: any keypress on any input inside the Formik should trigger the validation only for that specific input (not for others). It would be like the same effect like typing something in that input and then hitting Enter (because when hitting Enter we trigger the validation). But I want that to happen without hitting Enter.

Any idea on how to do that in a proper way?

Thanks!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can render Field with a child component such as <input> and handle its onKeyDown event. Rendering it this way gives you access to Formik's props which you can use to trigger validation.

         <Field name="email">
          {({ field, form }) => (
            <input
              onKeyDown={() => form.setFieldTouched('email')}
              {...field}
              id="email"
            />
          )}
        </Field>
        <ErrorMessage name="email">
          {(error) => <div style={{ color: '#f00' }}>{error}</div>}
        </ErrorMessage>
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