I read this solution but that doesn't really answer the question. I am using formik and semantic-ui-react. Is is possible to use Yup with semantic-ui-react? If yes could someone provide an example?
Asked
Active
Viewed 1,724 times
1 Answers
6
yes it is possible , Here is one way to do it. paste this code in codesandbox.io and install the dependencies like formik yup and semantic-ui-react
import React from "react";
import { render } from "react-dom";
import { Formik, Field } from "formik";
import * as yup from "yup";
import { Button, Checkbox, Form } from "semantic-ui-react";
const styles = {
fontFamily: "sans-serif",
textAlign: "center"
};
const App = () => (
<>
<Formik
initialValues={{
firstname: "",
lastname: ""
}}
onSubmit={values => {
alert(JSON.stringify(values));
}}
validationSchema={yup.object().shape({
firstname: yup.string().required("This field is required"),
lastname: yup.string().required()
})}
render={({
values,
errors,
touched,
handleChange,
handleBlur,
handleSubmit
}) => {
return (
<Form>
<Form.Field>
<label>First Name</label>
<input
placeholder="First Name"
name="firstname"
onChange={handleChange}
onBlur={handleBlur}
/>
</Form.Field>
{touched.firstname && errors.firstname && (
<div> {errors.firstname}</div>
)}
<Form.Field>
<label>Last Name</label>
<input
placeholder="Last Name"
name="lastname"
onChange={handleChange}
onBlur={handleBlur}
/>
</Form.Field>
{touched.lastname && errors.lastname && (
<div> {errors.lastname}</div>
)}
<Form.Field>
<Checkbox label="I agree to the Terms and Conditions" />
</Form.Field>
<Button type="submit" onClick={handleSubmit}>
Submit
</Button>
</Form>
);
}}
/>
</>
);
render(<App />, document.getElementById("root"));

Rahil Ahmad
- 3,056
- 1
- 16
- 21
-
`TypeError: _this.props.onSubmit is not a function` error on submit. – The Coder Apr 15 '19 at 15:37
-
I have edited the answer to include onSubmit attribute in
@TheCoder – Rahil Ahmad Apr 16 '19 at 08:38 -
@RahilAhmad is there any way i can send a parameter to handlesubmit that i can later on recieve in onsubmit? – Rishi Sahu Feb 18 '21 at 10:02
-
@RishiSahu you can make that part of initial state, that way you can update that from helpers and receive it in on submit internally or you can manually call the handleSumbit with values with any thing else you want to pass to ti – Rahil Ahmad Feb 20 '21 at 14:45
-
@RahilAhmad I did try to pass the params to handleSubmit but I don't receive those params in onSubmit function. – Rishi Sahu Feb 20 '21 at 15:54