Reputation:
I implement forms using the "Formik" library
I have this code:
import { useFormik } from 'formik';
import * as yup from 'yup';
import FInput from './FInput/FInput';
const Form = () => {
const {handleSubmit, values, handleChange} = useFormik({
initialValues: {
username: '',
email: '',
password: '',
confirm_password: '',
},
validationSchema: yup.object().shape( schema: {
username: yup.string() // the error here
.required('This field is required'),
email: yup.string()
.required('This field is required')
.email('This field is required'),
password: yup.string()
.required('This field is required')
.min(6, 'This password is too short')
.max(30, 'This passwors is too long'),
confirm_password: yup.string()
.oneOf([yup.ref(key:'password'), null])
.required('This field is required'),
}),
onSubmit: (formValues) => {
console.log('submit', formValues);
},
});
return (
<form className="fform" onSubmit={handleSubmit}>
<FInput
label="username"
id="username"
inputProps={{
name:'username',
value: values.username,
onChange: handleChange,
}}
/>
<FInput
label="email"
id="email"
inputProps={{
name:'email',
value: values.email,
onChange: handleChange,
type: 'email',
}}
/>
<FInput
label="password"
id="password"
inputProps={{
name:'password',
value: values.password,
onChange: handleChange,
type:'password',
}}
/>
<FInput
label="Confirm password"
id="confirm_password"
inputProps={{
name:'confirm_password',
value: values.confirm_password,
onChange: handleChange,
type:'password',
}}
/>
<button type="submit">Submit Form</button>
</form>
);
};
export default Form;
I have error:
Line 18:25: Parsing error: Unexpected reserved type string
18 | username: yup.string()
By the way, username
is not highlighted in color in text editor.
How to fix error? I have looked at the code many times and cannot understand what caused the error.Please help resolve this issue.
Upvotes: 1
Views: 78
Reputation: 19194
object.shape
API takes as an argument an object with fields in them.
The Yup definition for Formik has to be:
validationSchema: yup.object().shape({
username: yup.string()
.required('This field is required'),
email: yup.string()
.required('This field is required')
.email('This field is required'),
password: yup.string()
.required('This field is required')
.min(6, 'This password is too short')
.max(30, 'This passwors is too long'),
confirm_password: yup.string()
.oneOf([yup.ref(key:'password'), null])
.required('This field is required'),
})
Upvotes: 1