Skip to content

Instantly share code, notes, and snippets.

@jucian0
Last active June 9, 2020 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jucian0/aa979905723b7172ff5ec001370499ee to your computer and use it in GitHub Desktop.
Save jucian0/aa979905723b7172ff5ec001370499ee to your computer and use it in GitHub Desktop.
const initialValues = {
name:"Jesse",
email:"jesse@jesse.com",
pets:["felix"],
accept:false
}
const validation = yup.object().shape({
name: yup.string().required("This field is required"),
email: yup.string().email("This field must be a valid e-mail").required("This field is required"),
pets: yup.array(yup.string().required("This field is required")),
accept:yup.bool().oneOf([true], 'Field must be checked')
})
function ControlledForm(){
const [{values, reset},{input}] = useForm({debounce:300, initialValues, validation})
return (
<>
<input {...input("name", "text")}/>
<span>{touched.name && errors.name}</span>
<input {...input("email" ,"email")}/>
<span>{touched.email && errors.email}</span>
<input {...input("pets.0" ,"text")}/>
<span>{touched.pets?.[0] && errors.pets?.[0]}</span>
<input {...input("accept", "checkbox")}/>
<span>{touched.accept && errors.accept}</span>
</>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment