Commit e0075d44 authored by Yannis Barlas's avatar Yannis Barlas

fix(submission form): validate co-authors correctly

parent 0b0e2148
......@@ -2,7 +2,7 @@
import React from 'react'
import styled from 'styled-components'
import { get, omit } from 'lodash'
import { get, has, omit } from 'lodash'
import { Icon as UIIcon } from '@pubsweet/ui'
import { th } from '@pubsweet/ui-toolkit'
......@@ -135,6 +135,7 @@ const AuthorInput = props => {
name={affiliationsName}
value={get(values, affiliationsName)}
{...autoCompleteProps}
error={has(errors, affiliationsName)}
placeholder="The author's affiliations" // override placeholder
/>
</AffiliationsWrapper>
......
......@@ -127,9 +127,17 @@ const TextFieldGroup = props => {
let err
if (Array.isArray(error)) {
const firstErr = error.find(e => !isEmpty(e))
const firstKey = keys(firstErr) && keys(firstErr)[0]
err = firstErr && firstErr[firstKey]
const firstItemWithErr = error.find(e => !isEmpty(e))
if (name === 'coAuthors') {
err =
get(firstItemWithErr, 'name') ||
get(firstItemWithErr, 'credit') ||
get(firstItemWithErr, 'affiliations')
} else {
const firstKey = keys(firstItemWithErr) && keys(firstItemWithErr)[0]
err = firstItemWithErr && firstItemWithErr[firstKey]
}
} else {
err = error
}
......
......@@ -12,6 +12,19 @@ const stripHTML = html => {
return tmp.textContent || tmp.innerText || ''
}
const isCreditEmpty = arr => {
if (!arr) return true
if (arr.length === 0) return true
if (arr.length === 1 && arr[0] === '') return true
return false
}
const isStringFieldEmpty = str => {
if (!str) return true
if (str.length === 0) return true
return false
}
// eslint-disable-next-line func-names, prefer-arrow-callback, object-shorthand
const validateWBExists = function(val) {
if (!val) return true
......@@ -46,24 +59,58 @@ const initial = {
}),
coAuthors: yup.array(
yup.object().shape({
affiliations: yup.string().required('Must provide author affiliations'),
affiliations: yup.string().test(
'co-auhtor affiliations pass if all fields are empty',
'Affiliations are required for all authors',
// eslint-disable-next-line func-names, prefer-arrow-callback
function(val) {
const { credit, name } = this.parent
if (isCreditEmpty(credit) && isStringFieldEmpty(name)) return true
if (isStringFieldEmpty(val)) return false
return true
},
),
credit: yup
.array()
.of(yup.string())
.test(
'coauthor has credit',
'Must choose credit for all authors',
'Credit is required for all authors',
// eslint-disable-next-line func-names, prefer-arrow-callback
function(val) {
const { name } = this.parent
if (!name) return true
if (name && (!val || val.length === 0)) return false
const { affiliations, name } = this.parent
if (isStringFieldEmpty(affiliations) && isStringFieldEmpty(name)) {
return true
}
if (isCreditEmpty(val)) return false
return true
},
)
.nullable(),
name: yup
.string()
.test(
'co-author pass if all fields empty',
'Name is required for all authors',
// eslint-disable-next-line func-names, prefer-arrow-callback
function(val) {
if (val) return true
const { affiliations, credit } = this.parent
if (
(!affiliations || affiliations.length === 0) &&
isCreditEmpty(credit)
) {
return true
}
return false
},
)
.test(
'is co-author valid',
'Must be a registered WormBase Person',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment