Original example: Redux Form - Synchronous Validation Example
Last active
October 26, 2017 13:26
-
-
Save shinaisan/c602dea4a92632b118c55e026384e3e1 to your computer and use it in GitHub Desktop.
Redux Form - Synchronous Validation Example
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 from 'react'; | |
import SyncValidationForm from './SyncValidationForm'; | |
import { createStore } from 'redux'; | |
import { Provider } from 'react-redux'; | |
import reducer from './reducers'; | |
import "bootstrap/dist/css/bootstrap.css"; | |
const store = createStore(reducer); | |
class App extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = {}; | |
this.setState({debug: ''}); | |
} | |
submit(values) { | |
this.setState({debug: JSON.stringify(values)}); | |
} | |
render() { | |
const submit = this.submit.bind(this); | |
return ( | |
<Provider store={store}> | |
<div> | |
<SyncValidationForm onSubmit={submit} /> | |
<div> | |
<pre>{this.state.debug}</pre> | |
</div> | |
</div> | |
</Provider> | |
); | |
} | |
} | |
export default App; | |
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
#!/bin/bash | |
NAME=redux-form-sync-validation | |
echo -n "This script runs create-react-app $NAME. Proceed? (Y/N) " | |
read YN | |
if [ x$YN != xY ] | |
then | |
echo "Bye." | |
exit | |
fi | |
# Delete this line only if you are sure what is done from this line on. | |
exit | |
create-react-app $NAME | |
cd $NAME | |
cp -v ../package.json . | |
cd src | |
rm -f App.* logo.svg | |
cp -v ../../*.js . | |
cd .. | |
yarn install | |
echo 'To launch the dev server, run `yarn run start`.' | |
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
{ | |
"name": "redux-form-sync-validation", | |
"version": "0.1.0", | |
"private": true, | |
"dependencies": { | |
"bootstrap": "^3.3.7", | |
"react": "^16.0.0", | |
"react-bootstrap": "^0.31.5", | |
"react-dom": "^16.0.0", | |
"react-redux": "^5.0.6", | |
"redux": "^3.7.2", | |
"redux-form": "^7.1.2" | |
}, | |
"devDependencies": { | |
"react-scripts": "1.0.14" | |
}, | |
"scripts": { | |
"start": "react-scripts start", | |
"build": "react-scripts build", | |
"test": "react-scripts test --env=jsdom", | |
"eject": "react-scripts eject" | |
} | |
} |
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 { combineReducers } from 'redux'; | |
import { reducer as formReducer } from 'redux-form'; | |
const rootReducer = combineReducers({ | |
// ...your other reducers here | |
// you have to pass formReducer under 'form' key, | |
// for custom keys look up the docs for 'getFormState' | |
form: formReducer | |
}); | |
export default rootReducer; | |
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 from 'react'; | |
import { Field, reduxForm } from 'redux-form'; | |
import { | |
Form, FormGroup, | |
Grid, Row, Col, | |
Button | |
} from 'react-bootstrap'; | |
const validate = values => { | |
const errors = {} | |
if (!values.username) { | |
errors.username = 'Required' | |
} else if (values.username.length > 15) { | |
errors.username = 'Must be 15 characters or less' | |
} | |
if (!values.email) { | |
errors.email = 'Required' | |
} else if (!/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(values.email)) { | |
errors.email = 'Invalid email address' | |
} | |
if (!values.age) { | |
errors.age = 'Required' | |
} else if (isNaN(Number(values.age))) { | |
errors.age = 'Must be a number' | |
} else if (Number(values.age) < 18) { | |
errors.age = 'Sorry, you must be at least 18 years old' | |
} | |
return errors | |
}; | |
const warn = values => { | |
const warnings = {} | |
if (values.age < 19) { | |
warnings.age = 'Hmm, you seem a bit young...' | |
} | |
return warnings | |
}; | |
const renderField = ({ | |
input, | |
label, | |
type, | |
meta: { touched, error, warning } | |
}) => ( | |
<FormGroup> | |
<Grid> | |
<Row> | |
<Col sm={2}> | |
<label>{label}</label> | |
</Col> | |
<Col sm={8}> | |
<input {...input} placeholder={label} type={type} size="48" /> | |
{ | |
touched && | |
((error && <span>{error}</span>) || | |
(warning && <span>{warning}</span>)) | |
} | |
</Col> | |
</Row> | |
</Grid> | |
</FormGroup> | |
); | |
const SyncValidationForm = props => { | |
const { handleSubmit, pristine, reset, submitting } = props; | |
return ( | |
<Form onSubmit={handleSubmit} horizontal > | |
<Field | |
name="username" | |
type="text" | |
component={renderField} | |
label="Username" | |
/> | |
<Field name="email" type="email" component={renderField} label="Email" /> | |
<Field name="age" type="number" component={renderField} label="Age" /> | |
<div> | |
<Button type="submit" bsStyle="primary" disabled={submitting}> | |
Submit | |
</Button> | |
<Button type="button" bsStyle="default" | |
disabled={pristine || submitting} onClick={reset}> | |
Clear Values | |
</Button> | |
</div> | |
</Form> | |
); | |
}; | |
export default reduxForm({ | |
form: 'syncValidation', // a unique identifier for this form | |
validate, // <--- validation function given to redux-form | |
warn // <--- warning function given to redux-form | |
})(SyncValidationForm); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment