-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pkp/pkp-lib#9996 Initial attempt to ignore hidden fields for validate… (
#357) * pkp/pkp-lib#9996 Remove error for input that gets hidden * pkp/pkp-lib#9996 check only visible fields for required * pkp/pkp-lib#9996 Handle correctly multiple fields removing error synchronously * pkp/pkp-lib#9996 add jsdocs
- Loading branch information
1 parent
aa3b909
commit 655e37f
Showing
5 changed files
with
90 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* Should a field be shown? | ||
* | ||
* @param {Object} field One of this.fields | ||
* @return {Boolean} | ||
*/ | ||
export function shouldShowFieldWithinGroup(field, allFields) { | ||
if (typeof field.showWhen === 'undefined') { | ||
return true; | ||
} | ||
const whenFieldName = | ||
typeof field.showWhen === 'string' ? field.showWhen : field.showWhen[0]; | ||
const whenField = allFields.find((field) => field.name === whenFieldName); | ||
if (!whenField) { | ||
return false; | ||
} | ||
if (typeof field.showWhen === 'string') { | ||
return !!whenField.value; | ||
} | ||
return whenField.value === field.showWhen[1]; | ||
} | ||
/** | ||
* Should a group be shown? | ||
* | ||
* @param {Object} group One of this.groups | ||
* @return {Boolean} | ||
*/ | ||
export function shouldShowGroup(group, fields) { | ||
if (typeof group.showWhen === 'undefined') { | ||
return true; | ||
} | ||
const whenFieldName = | ||
typeof group.showWhen === 'string' ? group.showWhen : group.showWhen[0]; | ||
const whenField = fields.find((field) => field.name === whenFieldName); | ||
if (!whenField) { | ||
return false; | ||
} | ||
if (typeof group.showWhen === 'string') { | ||
return !!whenField.value; | ||
} | ||
return whenField.value === group.showWhen[1]; | ||
} | ||
|
||
/** | ||
* Should a field be shown? | ||
* | ||
* @param {Object} field One field to check | ||
* @param {Object} fields All form fields | ||
* @param {Object} groups All form groups | ||
* @return {Boolean} | ||
*/ | ||
|
||
export function shouldShowField(field, fields, groups) { | ||
const group = groups.find((group) => group.id === field.groupId); | ||
|
||
return ( | ||
shouldShowGroup(group, fields) && shouldShowFieldWithinGroup(field, fields) | ||
); | ||
} |