Last active
September 30, 2017 11:29
-
-
Save chansuke/e528354c90f93636b51a004e76499ed3 to your computer and use it in GitHub Desktop.
redux-formでclient validation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, {Component} from 'react'; | |
import {reduxForm} from 'redux-form'; | |
class LoginForm extends Component { | |
onSubmit(props) { | |
//do your submit stuff | |
} | |
render() { | |
const {fields: {email}, handleSubmit} = this.props; | |
return ( | |
<form onSubmit={handleSubmit(this.onSubmit.bind(this))}> | |
<input type="text" placeholder="Email" | |
className={`form-control ${email.touched && email.invalid ? 'has-error' : '' }`} | |
{...email} | |
/> | |
<span className="text-help"> | |
{email.touched ? email.error : ''} | |
</span> | |
<input type="submit"/> | |
</form> | |
); | |
} | |
} | |
function validation(values) { | |
const errors = {}; | |
const emailPattern = /(.+)@(.+){2,}\.(.+){2,}/; | |
if (!emailPattern.test(values.email)) { | |
errors.email = 'Enter a valid email'; | |
} | |
return errors; | |
} | |
LoginForm = reduxForm({ | |
form: 'LoginForm', | |
fields: ['email'], | |
validate: validation | |
}, null, null)(LoginForm); | |
export default LoginForm; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment