Skip to content

Instantly share code, notes, and snippets.

@kilgarenone
Last active April 24, 2020 03:07
Show Gist options
  • Save kilgarenone/f6945ff3d67756df5338b35d9f8bd0b3 to your computer and use it in GitHub Desktop.
Save kilgarenone/f6945ff3d67756df5338b35d9f8bd0b3 to your computer and use it in GitHub Desktop.
yup in a redux-form component
import * as React from 'react'
import * as Yup from 'yup'
import { Field, reduxForm } from 'redux-form'
import validator from './validator.ts'
// more imports
class YourClass extends React.Component<Props> {
// stuffs
render() {
return (
<form onSubmit={this.onSubmit}>
<Field
name={'email'}
component={TextInput}
placeholder={'abc@example.com'}
/>
</form>
)
}
}
const schema = Yup.object().shape({
email: Yup
.string()
.email('Invalid email address')
.required('Email address is required')
})
export default reduxForm({
form: 'yourFormName',
asyncValidate: validator(schema)
})(YourClass)
@shamseerahammedm
Copy link

Whats inside the validator.ts file, its shows validator error when i do this ?

@kilgarenone
Copy link
Author

kilgarenone commented Apr 24, 2020

@shamseerahammedm

Whats inside the validator.ts file, its shows validator error when i do this ?

Hi. The content of validator.ts can be found here https://gist.github.com/kilgarenone/cef4d29ffdf1381bef30d2333372f7c7#file-validator-ts

I also talked about this whole thing in this medium post https://medium.com/@kilgarenone/use-yup-with-redux-form-684d6c23ab41

Good luck!

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