Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to validate few fields (not all) on type button click in yup and formik. #3970

Open
Rahulnagarwal opened this issue May 10, 2024 · 2 comments

Comments

@Rahulnagarwal
Copy link

const initialValues = {
salutation: '',
firstName: "",
lastName: "",
email: '',
role: "",
employerName: "",
countryId: "",
stateId: '',
password: '',
confirmPassword: '',
};

const validationSchema = Yup.object({
    firstName: Yup.string().required('First name is required'),
    lastName: Yup.string().required('Last name is required'),
    email: Yup.string().required('Email is required'),
    role: Yup.string().required('Role is required'),
    employerName: Yup.string().required('Employer Name is required'),
    countryId: Yup.string().required('Country is required'),
    stateId: Yup.string().required('State is required'),
    password: Yup.string().required('State is required'),
    confirmPassword: Yup.string().required('State is required'),
});

const onSubmit = async (values: any) => {};

const formik = useFormik({
initialValues,
validationSchema,
onSubmit,
});

now how can i validate fields except password and confirmPassword on type button click?

@erashu212
Copy link

@Rahulnagarwal Please provide a code sandbox meanwhile

const validationSchema = Yup.object({
    firstName: Yup.string().required('First name is required'),
    lastName: Yup.string().required('Last name is required'),
    email: Yup.string().required('Email is required'),
    role: Yup.string().required('Role is required'),
    employerName: Yup.string().required('Employer Name is required'),
    countryId: Yup.string().required('Country is required'),
    stateId: Yup.string().required('State is required'),
    password: Yup.string(),
    confirmPassword: Yup.string(),
});

you can do this

@navinrangar
Copy link

If you don't want to verify password and confirmPassword fields, simply don't put them in the yup validation object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants