From 41df6fe837aacb96769b3ef4e4a24f39c1513b1a Mon Sep 17 00:00:00 2001 From: Brett Sun Date: Thu, 17 Dec 2015 13:05:39 +0100 Subject: [PATCH] Add global notification to form validation --- js/components/ascribe_forms/form.js | 6 ++--- .../pr_forms/pr_register_piece_form.js | 9 +++---- js/utils/form_utils.js | 26 +++++++++++++++++++ 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/js/components/ascribe_forms/form.js b/js/components/ascribe_forms/form.js index d4002e85..91d00f65 100644 --- a/js/components/ascribe_forms/form.js +++ b/js/components/ascribe_forms/form.js @@ -178,20 +178,20 @@ let Form = React.createClass({ let formData = this.getFormData(); // sentry shouldn't post the user's password - if(formData.password) { + if (formData.password) { delete formData.password; } console.logGlobal(err, formData); - if(this.props.isInline) { + if (this.props.isInline) { let notification = new GlobalNotificationModel(getLangText('Something went wrong, please try again later'), 'danger'); GlobalNotificationActions.appendGlobalNotification(notification); } else { this.setState({errors: [getLangText('Something went wrong, please try again later')]}); } - } + this.setState({submitted: false}); }, diff --git a/js/components/whitelabel/prize/portfolioreview/components/pr_forms/pr_register_piece_form.js b/js/components/whitelabel/prize/portfolioreview/components/pr_forms/pr_register_piece_form.js index 645d13a2..73c0d500 100644 --- a/js/components/whitelabel/prize/portfolioreview/components/pr_forms/pr_register_piece_form.js +++ b/js/components/whitelabel/prize/portfolioreview/components/pr_forms/pr_register_piece_form.js @@ -21,6 +21,7 @@ import requests from '../../../../../../utils/requests'; import { getErrorNotificationMessage } from '../../../../../../utils/error_utils'; import { setCookie } from '../../../../../../utils/fetch_api_utils'; +import { validateForms } from '../../../../../../utils/form_utils'; import { getLangText } from '../../../../../../utils/lang_utils'; import { formSubmissionValidation } from '../../../../../ascribe_uploader/react_s3_fine_uploader_utils'; @@ -55,7 +56,7 @@ const PRRegisterPieceForm = React.createClass({ * second adding all the additional details */ submit() { - if(!this.validateForms()) { + if (!this.validateForms()) { return; } @@ -127,11 +128,7 @@ const PRRegisterPieceForm = React.createClass({ additionalDataForm, uploadersForm } = this.refs; - const registerPieceFormValidation = registerPieceForm.validate(); - const additionalDataFormValidation = additionalDataForm.validate(); - const uploaderFormValidation = uploadersForm.validate(); - - return registerPieceFormValidation && additionalDataFormValidation && uploaderFormValidation; + return validateForms([registerPieceForm, additionalDataForm, uploadersForm], true); }, getCreateBlobRoutine() { diff --git a/js/utils/form_utils.js b/js/utils/form_utils.js index 8d12a8c1..0581b28b 100644 --- a/js/utils/form_utils.js +++ b/js/utils/form_utils.js @@ -2,8 +2,34 @@ import { getLangText } from './lang_utils'; +import GlobalNotificationActions from '../actions/global_notification_actions'; +import GlobalNotificationModel from '../models/global_notification_model'; + import AppConstants from '../constants/application_constants'; +/** + * Validates a given list of forms + * @param {Form} forms List of forms, each of which should have a `validate` method available + * @param {boolean} showFailureNotification Show global notification if there are validation failures + * @return {boolean} True if validation did *NOT* catch any errors + */ +export function validateForms(forms, showFailureNotification) { + const validationSuccessful = forms.reduce((result, form) => { + if (form && typeof form.validate === 'function') { + return form.validate() && result; + } else { + throw new Error('Form given for validation does not have a `validate` method'); + } + }, true); + + if (!validationSuccessful && showFailureNotification) { + const notification = new GlobalNotificationModel(getLangText('Oops, there may be missing or invalid fields. Please check your inputs again.'), 'danger'); + GlobalNotificationActions.appendGlobalNotification(notification); + } + + return validationSuccessful; +} + /** * Get the data ids of the given piece or editions. * @param {boolean} isPiece Is the given entities parameter a piece? (False: array of editions)