From 34c9bea967331f9e9c65f6aca02c5cd54a3fc52e Mon Sep 17 00:00:00 2001 From: Justin Littman Date: Tue, 23 Jul 2019 13:27:01 -0400 Subject: [PATCH] Oops, adds validation error display to InputURI. --- __tests__/__fixtures__/OnlyEquivalent.json | 2 +- .../components/editor/property/InputURI.test.js | 13 +++++++++++++ src/components/editor/property/InputURI.jsx | 16 ++++++++++------ 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/__tests__/__fixtures__/OnlyEquivalent.json b/__tests__/__fixtures__/OnlyEquivalent.json index 7e909e780..044f46764 100644 --- a/__tests__/__fixtures__/OnlyEquivalent.json +++ b/__tests__/__fixtures__/OnlyEquivalent.json @@ -4,7 +4,7 @@ "id": "resourceTemplate:bf2:OnlyEquivalent", "propertyTemplates": [ { - "mandatory": "false", + "mandatory": "true", "repeatable": "true", "type": "resource", "resourceTemplates": [], diff --git a/__tests__/components/editor/property/InputURI.test.js b/__tests__/components/editor/property/InputURI.test.js index 8ad3517b5..58f5ca519 100644 --- a/__tests__/components/editor/property/InputURI.test.js +++ b/__tests__/components/editor/property/InputURI.test.js @@ -213,3 +213,16 @@ describe('when there is a default literal value in the property template', () => }) }) }) + +describe('Errors', () => { + const errors = ['Required'] + const wrapper = shallow() + + it('displays the errors', () => { + expect(wrapper.find('span.help-block').text()).toEqual('Required') + }) + + it('sets the has-error class', () => { + expect(wrapper.exists('div.has-error')).toEqual(true) + }) +}) diff --git a/src/components/editor/property/InputURI.jsx b/src/components/editor/property/InputURI.jsx index 332ff4ab8..6be898269 100644 --- a/src/components/editor/property/InputURI.jsx +++ b/src/components/editor/property/InputURI.jsx @@ -6,8 +6,11 @@ import SinopiaPropTypes from 'SinopiaPropTypes' import { connect } from 'react-redux' import shortid from 'shortid' import { removeItem, itemsSelected } from 'actions/index' -import { findNode, getDisplayValidations, getPropertyTemplate } from 'selectors/resourceSelectors' +import { + findNode, getDisplayValidations, getPropertyTemplate, findErrors, +} from 'selectors/resourceSelectors' import { booleanPropertyFromTemplate, isValidURI } from 'Utilities' +import _ from 'lodash' const InputURI = (props) => { // Don't render if don't have property templates yet. @@ -72,8 +75,6 @@ const InputURI = (props) => { * @return {bool} true if the field should be marked as required (e.g. not all obligations met) */ const required = booleanPropertyFromTemplate(props.propertyTemplate, 'mandatory', false) - && props.formData.errors - && props.formData.errors.length !== 0 const items = props.items || [] @@ -103,13 +104,13 @@ const InputURI = (props) => { }) - const error = props.displayValidations && required ? 'Required' : undefined + let error let groupClasses = 'form-group' - if (error) { + if (props.displayValidations && !_.isEmpty(props.errors)) { groupClasses += ' has-error' + error = props.errors.join(',') } - return (
{ @@ -154,12 +156,14 @@ const mapStateToProps = (state, props) => { // items has to be its own prop or rerendering won't occur when one is removed const items = formData.items const propertyTemplate = getPropertyTemplate(state, resourceTemplateId, propertyURI) + const errors = findErrors(state.selectorReducer, reduxPath) return { formData, items, propertyTemplate, displayValidations, + errors, } }