The original example: Redux Form - Async Blur Validation Example
Last active
October 27, 2017 14:35
-
-
Save shinaisan/d7628420809130a226e570f32e936f86 to your computer and use it in GitHub Desktop.
Redux Form - Async Blur 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 AsyncValidationForm from './AsyncValidationForm'; | |
import { createStore } from 'redux'; | |
import { Provider } from 'react-redux'; | |
import reducer from './reducer'; | |
import "bootstrap/dist/css/bootstrap.css"; | |
const store = createStore(reducer); | |
class App extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = {debug: ''}; | |
} | |
handleSubmit(values) { | |
const self = this; | |
self.setState({debug: JSON.stringify(values)}); | |
} | |
render() { | |
const onSubmit = this.handleSubmit.bind(this); | |
return ( | |
<Provider store={store}> | |
<div> | |
<AsyncValidationForm onSubmit={onSubmit} /> | |
<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
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)) | |
const asyncValidate = (values /*, dispatch */) => { | |
return sleep(1000).then(() => { | |
// simulate server latency | |
if (['john', 'paul', 'george', 'ringo'].includes(values.username)) { | |
const obj = { username: 'That username is taken' } | |
throw obj; | |
} | |
}) | |
} | |
export default asyncValidate |
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 validate from './validate' | |
import asyncValidate from './asyncValidate' | |
import { | |
Form, FormGroup, | |
Row, Col, | |
Button | |
} from 'react-bootstrap' | |
import 'font-awesome/css/font-awesome.css' | |
import FontAwesome from 'react-fontawesome' | |
const renderField = ({ | |
input, | |
label, | |
type, | |
meta: { asyncValidating, touched, error } | |
}) => ( | |
<FormGroup> | |
<Row> | |
<Col sm={2}> | |
<label>{label}</label> | |
</Col> | |
<Col sm={8}> | |
<input {...input} type={type} placeholder={label} size="40" /> | |
<span> | |
{ | |
asyncValidating | |
? <FontAwesome name="spinner" spin /> | |
: '' | |
} | |
</span> | |
{touched && error && <span>{error}</span>} | |
</Col> | |
</Row> | |
</FormGroup> | |
) | |
const AsyncValidationForm = props => { | |
const { handleSubmit, pristine, reset, submitting, asyncValidating } = props | |
return ( | |
<Form onSubmit={handleSubmit}> | |
<Field | |
name="username" | |
type="text" | |
component={renderField} | |
label="Username" | |
/> | |
<Field | |
name="password" | |
type="password" | |
component={renderField} | |
label="Password" | |
/> | |
<div> | |
<Button type="submit" bsStyle="primary" | |
disabled={asyncValidating || submitting}> | |
Sign Up | |
</Button> | |
<Button type="button" bsStyle="default" | |
disabled={pristine || submitting} onClick={reset}> | |
Clear Values | |
</Button> | |
</div> | |
</Form> | |
) | |
} | |
export default reduxForm({ | |
form: 'asyncValidation', // a unique identifier for this form | |
validate, | |
asyncValidate, | |
asyncBlurFields: ['username'] | |
})(AsyncValidationForm) |
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-async-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-async-validation", | |
"version": "0.1.0", | |
"private": true, | |
"dependencies": { | |
"bootstrap": "^3.3.7", | |
"font-awesome": "^4.7.0", | |
"react": "^16.0.0", | |
"react-bootstrap": "^0.31.5", | |
"react-dom": "^16.0.0", | |
"react-fontawesome": "^1.6.1", | |
"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
const validate = values => { | |
const errors = {} | |
if (!values.username) { | |
errors.username = 'Required' | |
} | |
if (!values.password) { | |
errors.password = 'Required' | |
} | |
return errors | |
} | |
export default validate |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment