ducky/web
ducky/web/src/components/validation-error.jsx
Update our ValidationError component to accept arrays. Allow our ValidationError component to match an array of fields, headers, or params. This is for components that may address multiple inputs (e.g., month/year inputs) but also lays the ground work for inverse-matching. Ideally, inverse-matching and matching ValidationErrors should mirror each other, logically, so the syntax is identical. But we also should have the common use case easily supported, so if you use field instead of fields (or header/headers, param/params) we'll automatically turn that into an array.
1 import React from 'react'
3 export default React.createClass({
4 displayName: 'ValidationError',
6 render () {
7 let fields = this.props.fields
8 if (this.props.field && !this.props.fields) {
9 fields = [this.props.field]
10 }
11 let params = this.props.params
12 if (this.props.param && !this.props.params) {
13 params = [this.props.param]
14 }
15 let headers = this.props.headers
16 if (this.props.header && !this.props.headers) {
17 headers = [this.props.header]
18 }
19 const outputs = this.props.outputs
20 const errors = this.props.errors
21 return (
22 <div className={errors.length ? '' : 'hidden' }>
23 {errors.map(error => {
24 let errorString = ''
25 if (!error.field && !error.param && !error.header) {
26 return ''
27 }
28 if (fields && error.field && fields.indexOf(error.field) < 0) {
29 return ''
30 }
31 if (params && error.param && params.indexOf(error.param) < 0) {
32 return ''
33 }
34 if (headers && error.header && headers.indexOf(error.header) < 0) {
35 return ''
36 }
37 if (outputs[error.error] == undefined) {
38 errorString = 'An unknown error occurred. Please contact support. Sorry.'
39 } else {
40 errorString = outputs[error.error]
41 }
42 const id = [error.field, error.param, error.header, error.error].join("|")
43 return <div key={id} className="flash-error validation"><span>{errorString}</span></div>
44 })}
45 </div>
46 )
47 }
48 })