Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/on all fields filled #42

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@
"validator": "^3.41.3",
"react-addons-create-fragment": "^0.14.7"
}
}
}
18 changes: 14 additions & 4 deletions src/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,13 @@ export default class Form extends InputContainer {

_validateInput(name) {
this._validateOne(name, this.getValues());

if (typeof this.props.validateAllCallback === 'function') {
let values = this.getValues();
let { isValid } = this._validateAll(values, false);

this.props.validateAllCallback(isValid);
}
}

_hasError(iptName) {
Expand All @@ -175,7 +182,7 @@ export default class Form extends InputContainer {
});
}

_validateOne(iptName, context) {
_validateOne(iptName, context, setError = true) {
let input = this._inputs[iptName];

if (Array.isArray(input)) {
Expand Down Expand Up @@ -210,12 +217,14 @@ export default class Form extends InputContainer {
}
}

this._setError(iptName, !isValid, error);
if (setError) {
this._setError(iptName, !isValid, error);
}

return isValid;
}

_validateAll(context) {
_validateAll(context, setError = true) {
let isValid = true;
let errors = [];

Expand All @@ -233,7 +242,7 @@ export default class Form extends InputContainer {
}
} else {
Object.keys(this._inputs).forEach(iptName => {
if (!this._validateOne(iptName, context)) {
if (!this._validateOne(iptName, context, setError)) {
isValid = false;
errors.push(iptName);
}
Expand Down Expand Up @@ -331,6 +340,7 @@ Form.propTypes = {
onInvalidSubmit: React.PropTypes.func,
validateOne : React.PropTypes.func,
validateAll : React.PropTypes.func,
validateAllCallback: React.PropTypes.func,
validationEvent: React.PropTypes.oneOf([
'onChange', 'onBlur', 'onFocus'
]),
Expand Down