Skip to content

Commit

Permalink
Move error generation to GiftedManager (Programmatic Submit) (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
sambwest authored and cooperka committed Jun 23, 2017
1 parent 6e42d87 commit 3708c05
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 34 deletions.
21 changes: 21 additions & 0 deletions GiftedFormManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
23 changes: 12 additions & 11 deletions widgets/GooglePlacesWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<GooglePlacesAutocomplete
placeholder='Type a city name'
Expand All @@ -29,6 +29,7 @@ module.exports = React.createClass({
this._onChange({
name: details.formatted_address,
placeId: details.place_id,
details: details,
loc: [details.geometry.location.lng, details.geometry.location.lat],
types: details.types
});
Expand All @@ -46,7 +47,7 @@ module.exports = React.createClass({
color: '#1faadb',
},
}}

currentLocation={true} // Will add a 'Current location' button at the top of the predefined places list
currentLocationLabel="Current location"
currentLocationAPI='GoogleReverseGeocoding' // Which API to use: GoogleReverseGeocoding or GooglePlacesSearch
Expand All @@ -58,15 +59,15 @@ module.exports = React.createClass({
rankby: 'distance',
types: 'food',
}}


filterReverseGeocodingByTypes={['locality', 'administrative_area_level_3']} // filter the reverse geocoding results by types - ['locality', 'administrative_area_level_3'] if you want to display only cities

// predefinedPlaces={[everywhere]}


{...this.props} // @todo test sans (need for 'name')
/>
);
},
});
});
29 changes: 6 additions & 23 deletions widgets/SubmitWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: []});
},
Expand All @@ -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);
}
},
Expand Down

0 comments on commit 3708c05

Please sign in to comment.