From 3708c0547d7b27f2a440818b2024cd698960d460 Mon Sep 17 00:00:00 2001 From: Sam West Date: Thu, 22 Jun 2017 22:13:50 -0500 Subject: [PATCH] Move error generation to GiftedManager (Programmatic Submit) (#99) --- GiftedFormManager.js | 21 +++++++++++++++++++++ widgets/GooglePlacesWidget.js | 23 ++++++++++++----------- widgets/SubmitWidget.js | 29 ++++++----------------------- 3 files changed, 39 insertions(+), 34 deletions(-) diff --git a/GiftedFormManager.js b/GiftedFormManager.js index a74bc57..9bcb423 100644 --- a/GiftedFormManager.js +++ b/GiftedFormManager.js @@ -297,6 +297,27 @@ class Manager { this.stores[obj.formName].values[obj.name] = obj.value; } } + + getValidationErrors(validated, notValidMessage = '{TITLE} Invalid', requiredMessage = '{TITLE} Required') { + var errors = []; + if (validated.isValid === false) { + for (var k in validated.results) { + if (validated.results.hasOwnProperty(k)) { + for (var j in validated.results[k]) { + if (validated.results[k].hasOwnProperty(j)) { + if (validated.results[k][j].isValid === false) { + let defaultMessage = !!validated.results[k][j].value ? notValidMessage : requiredMessage; + errors.push(validated.results[k][j].message || defaultMessage.replace('{TITLE}', validated.results[k][j].title)); + // displaying only 1 error per widget + break; + } + } + } + } + } + } + return errors; + } } module.exports = new Manager(); diff --git a/widgets/GooglePlacesWidget.js b/widgets/GooglePlacesWidget.js index 913d0bb..a3f39cb 100644 --- a/widgets/GooglePlacesWidget.js +++ b/widgets/GooglePlacesWidget.js @@ -7,17 +7,17 @@ var {GooglePlacesAutocomplete} = require('react-native-google-places-autocomplet module.exports = React.createClass({ mixins: [WidgetMixin], - + getDefaultProps() { return { type: 'GooglePlacesWidget', }; }, - + render() { const everywhere = {description: 'Everywhere', geometry: { location: { lat: 0, lng: 0 } }}; - - + + return ( ); }, -}); \ No newline at end of file +}); diff --git a/widgets/SubmitWidget.js b/widgets/SubmitWidget.js index 4889b28..8649e29 100644 --- a/widgets/SubmitWidget.js +++ b/widgets/SubmitWidget.js @@ -41,28 +41,6 @@ module.exports = React.createClass({ }; }, - onValidationError(validated) { - var errors = []; - if (validated.isValid === false) { - for (var k in validated.results) { - if (validated.results.hasOwnProperty(k)) { - for (var j in validated.results[k]) { - if (validated.results[k].hasOwnProperty(j)) { - if (validated.results[k][j].isValid === false) { - let defaultMessage = !!validated.results[k][j].value ? this.props.notValidMessage : this.props.requiredMessage; - errors.push(validated.results[k][j].message || defaultMessage.replace('{TITLE}', validated.results[k][j].title)); - // displaying only 1 error per widget - break; - } - } - } - } - } - } - - this.props.form.setState({errors}); - }, - clearValidationErrors() { this.props.form.setState({errors: []}); }, @@ -89,7 +67,12 @@ module.exports = React.createClass({ }); this.props.onSubmit(true, values, validationResults, this._postSubmit, this.props.navigator); } else { - this.onValidationError(validationResults); + var errors = GiftedFormManager.getValidationErrors( + validationResults, + this.props.notValidMessage, + this.props.requiredMesage + ); + this.props.form.setState({errors: errors}); this.props.onSubmit(false, values, validationResults, this._postSubmit, this.props.navigator); } },