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

218
Views
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 answers
Answer question

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 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!