Skip to content

Instantly share code, notes, and snippets.

@chansuke
Last active September 30, 2017 11:29
Show Gist options
  • Save chansuke/e528354c90f93636b51a004e76499ed3 to your computer and use it in GitHub Desktop.
Save chansuke/e528354c90f93636b51a004e76499ed3 to your computer and use it in GitHub Desktop.
redux-formでclient validation
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