Skip to content

Commit 6fd1010

Browse files
authored
Abstract pod/itemId/formCounter logic and handle undefined cases
1 parent 0eb9a21 commit 6fd1010

File tree

2 files changed

+37
-32
lines changed

2 files changed

+37
-32
lines changed

ui/js/dfv/src/hooks/useValidation.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import {
44
useDispatch,
55
} from '@wordpress/data';
66

7-
const useValidation = ( defaultRules = [], value, fieldName, strokeKey ) => {
7+
const useValidation = ( defaultRules = [], value, fieldName, storeKey ) => {
88
const [ validationRules, setValidationRules ] = useState( defaultRules );
99

1010
//Validation messages for this field
1111
const validationMessages = useSelect( ( select ) => {
12-
const currentMessages = select( strokeKey ).getValidationMessages();
12+
const currentMessages = select( storeKey ).getValidationMessages();
1313
//Return for this field
1414
if ( currentMessages.hasOwnProperty( fieldName ) ) {
1515
return currentMessages[ fieldName ];
@@ -18,12 +18,12 @@ const useValidation = ( defaultRules = [], value, fieldName, strokeKey ) => {
1818
}, [ fieldName ] );
1919

2020
//Set validation messages for this field
21-
const { setValidationMessages } = useDispatch( strokeKey );
21+
const { setValidationMessages } = useDispatch( storeKey );
2222
const needsValidation = useSelect( ( select ) => {
23-
return select( strokeKey ).getNeedsValidating();
23+
return select( storeKey ).getNeedsValidating();
2424
}, [] );
2525

26-
const toggleNeedsValidating = useDispatch( strokeKey ).toggleNeedsValidating();
26+
const toggleNeedsValidating = useDispatch( storeKey ).toggleNeedsValidating();
2727

2828
useEffect( () => {
2929
const newMessages = [];

ui/js/dfv/src/pods-dfv.js

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -600,51 +600,56 @@ window.PodsDFV = {
600600
},
601601

602602
/**
603-
* Check if Pod being edited is valid.
603+
* Get list of validation messages for a form (based on pod, item ID, and form counter).
604604
*
605-
* Returns array of validation Messages
606-
*
607-
* @param {string} pod Pod slug/name.
608-
* @param {int} itemId Object ID.
609-
* @param {int} formCounter Form index.
605+
* @param {string|null} pod Pod slug/name. (Optional.)
606+
* @param {int|null} itemId Object ID. (Optional.)
607+
* @param {int|null} formCounter Form index. (Optional.)
610608
*
611-
* @return {string[]} Array of validation messages
609+
* @return {string[]|undefined} List of validation messages, or undefined if not found.
612610
*/
613-
checkValidation( pod, itemId, formCounter ) {
614-
const storeKey = createStoreKey(
611+
getValidationMessages( pod = null, itemId = null, formCounter = null ) {
612+
const form = this.detectForm(
615613
pod,
616614
itemId,
617-
formCounter,
618-
STORE_KEY_DFV
615+
formCounter
619616
);
620617

621-
const stored = select( storeKey );
622-
623-
// Store not found.
624-
if ( ! stored ) {
618+
if ( 'undefined' === typeof form ) {
625619
return undefined;
626620
}
627-
//dispatch toggleNeedsValidating
628-
dispatch( storeKey ).toggleNeedsValidating();
629-
//select getValidationMessages
630-
const validationMessages = select( storeKey ).getValidationMessages();
621+
622+
// Check validations.
623+
dispatch( form.storeKey ).toggleNeedsValidating();
624+
625+
// Get validation messages.
626+
const validationMessages = form.stored.getValidationMessages();
627+
628+
// Debug output for validation messages for now. @todo Remove this.
631629
console.log( { validationMessages } );
630+
632631
return validationMessages;
633632
},
634633

635634
/**
636-
* Check if Pod being edited is valid.
635+
* Check if the form is valid (based on pod, item ID, and form counter).
637636
*
638-
* @param {string} pod Pod slug/name.
639-
* @param {int} itemId Object ID.
640-
* @param {int} formCounter Form index.
637+
* @param {string|null} pod Pod slug/name. (Optional.)
638+
* @param {int|null} itemId Object ID. (Optional.)
639+
* @param {int|null} formCounter Form index. (Optional.)
641640
*
642-
* @return {boolean} True if valid, false if not.
641+
* @return {boolean|undefined} True if valid, false if not, or undefined if not found.
643642
*/
644-
isValid( pod, itemId, formCounter ) {
645-
const errors = this.checkValidation( pod, itemId, formCounter );
646-
return ! errors.length;
643+
formIsValid( pod = null, itemId = null, formCounter = null ) {
644+
const validationMessages = this.getValidationMessages( pod, itemId, formCounter );
645+
646+
if ( 'undefined' === typeof validationMessages ) {
647+
return undefined;
648+
}
649+
650+
return 0 === validationMessages.length;
647651
},
652+
648653
/**
649654
* Initialize Pod data.
650655
*

0 commit comments

Comments
 (0)