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

226
Visualizações
How do I validate if a state has been set using Yup validation?

I'm using Yup to check whether a field (that is set using the state 'groupSelected') has been selected by the user. (The field is a required field)

<Formik initialValues={{title:'', group:{groupSelected}}}
                        enableReinitialize='true'      // since we are setting the formik initial value to state, this is required to update the initial value when state changes
                        onSubmit={values => console.log(values)}
                        validationSchema={validationSchema}

I'm able to retrieve the selected values just fine when the form is submitted. However, I'm unsure how to validate if the state has actually been set or not.

    group: Yup.string().required()

I tried using the above but I guess it does not work because groupSelected is not a string.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Here is the basic example of Formik with yup. You can also try this way.

Example:

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

const SignupSchema = Yup.object().shape({
  firstName: Yup.string()
    .trim("spaces no t allowed")
    .strict()
    .required("Required"),
});

export const MyForm = () => (
  <div>
    <h1>Signup</h1>
    <Formik
      initialValues={{
        firstName: "",
      }}
      validationSchema={SignupSchema}
      onSubmit={(values) => {
        // same shape as initial values
        console.log(values);
      }}
    >
      {({ errors, values, touched, setFieldValue, registerField }) => (
        <Form>
          <Field 
            name="firstName" 
          />
          {errors.firstName && touched.firstName ? (
            <div>{errors.firstName}</div>
          ) : null}
          <br />
          {JSON.stringify(errors)}
          <button type="submit">Submit</button>
        </Form>
      )}
    </Formik>
  </div>
);
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