Skip to content

Commit

Permalink
Only allow submission when no fields have validation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
gnoesiboe committed Jan 28, 2016
1 parent 70def09 commit a5e9b83
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 8 deletions.
60 changes: 58 additions & 2 deletions src/js/lib/form/component/formComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class FormComponent extends React.Component {
static _getInitialState() {
return {
values: {},
errors: {}
validFields: {}
};
}

Expand All @@ -39,6 +39,30 @@ class FormComponent extends React.Component {
*/
componentDidMount() {
this._importInitialValuesStateFromChildFormElements();

this._disableSubmits();
}

/**
* @private
*/
_disableSubmits() {
this._childrenInstances.forEach(child => {
if (child instanceof FormSubmitComponent) {
child.disable();
}
});
}

/**
* @private
*/
_enableSubmits() {
this._childrenInstances.forEach(child => {
if (child instanceof FormSubmitComponent) {
child.enable();
}
});
}

/**
Expand Down Expand Up @@ -122,13 +146,45 @@ class FormComponent extends React.Component {
*/
_onFieldInvalid(fieldIdentifier) {
this._setFieldErrorState(fieldIdentifier, false);

this._resetSubmitStatus();
}

/**
* @private
*/
_onFieldValid(fieldIdentifier) {
this._setFieldErrorState(fieldIdentifier, true);

this._resetSubmitStatus();
}

/**
* @private
*/
_resetSubmitStatus() {
if (this._hasValidationErrors()) {
this._disableSubmits();
} else {
this._enableSubmits();
}
}

/**
* @returns {boolean}
*
* @private
*/
_hasValidationErrors() {
for (var key in this.state.validFields) {
if (this.state.validFields.hasOwnProperty(key)) {
if (this.state.validFields[key] === false) {
return true;
}
}
}

return false;
}

/**
Expand All @@ -138,7 +194,7 @@ class FormComponent extends React.Component {
* @private
*/
_setFieldErrorState(fieldIdentifier, isValid) {
var errors = this.state.errors;
var errors = this.state.validFields;
errors[fieldIdentifier] = isValid;

this.setState({
Expand Down
43 changes: 37 additions & 6 deletions src/js/lib/form/component/formSubmitComponent.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,41 @@
import React from 'react';
import FormElementComponent from './formElementComponent';
import { generateFormElementId } from './../helper/identifierHelper';
import _ from 'lodash';

/**
* @author Gijs Nieuwenhuis <gijs.nieuwenhuis@freshheads.com>
*/
class FormSubmitComponent extends React.Component {
class FormSubmitComponent extends FormElementComponent {

/**
* @param {Object} props
*/
constructor(props) {
super(props);

this.state = _.extend({}, this.state, {
isDisabled: this.props.isDisabled
});
}

/**
* @public
*/
disable() {
this.setState({
isDisabled: true
});
}

/**
* @public
*/
enable() {
this.setState({
isDisabled: false
})
}

/**
* @returns {XML}
Expand All @@ -15,22 +46,22 @@ class FormSubmitComponent extends React.Component {
id={generateFormElementId(this.props.identifier)}
name={this.props.identifier}
className={this.props.className}
disabled={this.props.isDisabled}>
disabled={this.state.isDisabled}>
{this.props.children}
</button>
);
}
}

FormSubmitComponent.defaultProps = {
FormSubmitComponent.defaultProps = _.extend({}, FormElementComponent.defaultProps, {
className: 'btn btn-success',
isDisabled: false
};
});

FormSubmitComponent.propTypes = {
FormSubmitComponent.propTypes = _.extend({}, FormElementComponent.propTypes, {
identifier: React.PropTypes.string.isRequired,
className: React.PropTypes.string.isRequired,
isDisabled: React.PropTypes.bool.isRequired
};
});

export default FormSubmitComponent;

0 comments on commit a5e9b83

Please sign in to comment.