Skip to content

Commit 4357886

Browse files
Shelob9sc0ttkclark
authored andcommitted
Put in needsValidating toggle
1 parent 0f090ff commit 4357886

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,23 @@ import {
77
const useValidation = ( defaultRules = [], value, fieldName, strokeKey ) => {
88
const [ validationRules, setValidationRules ] = useState( defaultRules );
99

10+
//Validation messages for this field
1011
const validationMessages = useSelect( ( select ) => {
1112
const currentMessages = select( strokeKey ).getValidationMessages();
12-
//has fieldName ?
13+
//Return for this field
1314
if ( currentMessages.hasOwnProperty( fieldName ) ) {
1415
return currentMessages[ fieldName ];
1516
}
1617
return [];
1718
}, [ fieldName ] );
1819

20+
//Set validation messages for this field
1921
const { setValidationMessages } = useDispatch( strokeKey );
22+
const needsValidation = useSelect( ( select ) => {
23+
return select( strokeKey ).getNeedsValidating();
24+
}, [] );
25+
26+
const toggleNeedsValidating = useDispatch( strokeKey ).toggleNeedsValidating();
2027

2128
useEffect( () => {
2229
const newMessages = [];
@@ -35,7 +42,10 @@ const useValidation = ( defaultRules = [], value, fieldName, strokeKey ) => {
3542
}
3643
} );
3744
setValidationMessages( fieldName, newMessages );
38-
}, [ value ] );
45+
if ( needsValidation ) {
46+
toggleNeedsValidating();
47+
}
48+
}, [ value, validationRules, needsValidation, toggleNeedsValidating, setValidationMessages ] );
3949

4050
const addValidationRules = ( rules = [] ) => {
4151
rules.forEach( ( rule ) => {

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -610,9 +610,8 @@ window.PodsDFV = {
610610
*
611611
* @returns {string[]} Array of validation messages
612612
*/
613-
checkValidation(pod, itemId, formCounter) {
613+
checkValidation( pod, itemId, formCounter ) {
614614
let messages = [];
615-
616615
return messages;
617616
},
618617

@@ -625,8 +624,8 @@ window.PodsDFV = {
625624
*
626625
* @returns {boolean} True if valid, false if not.
627626
*/
628-
isValid(pod, itemId, formCounter) {
629-
const errors = this.checkValidation(pod, itemId, formCounter);
627+
isValid( pod, itemId, formCounter ) {
628+
const errors = this.checkValidation( pod, itemId, formCounter );
630629
return !errors.length;
631630
},
632631
/**
@@ -784,7 +783,6 @@ window.PodsDFV = {
784783
// Create stores for each of the individual keys we found (the keys of
785784
// the initialStoresWithValues object).
786785
const initialStoreKeys = Object.keys(initialStoresWithValues);
787-
788786
const storeKeys = initialStoreKeys.map((storeKey) => {
789787
// The Edit Pod screen gets a different store set up than
790788
// other contexts.
@@ -793,12 +791,16 @@ window.PodsDFV = {
793791
window.podsAdminConfig,
794792
storeKey
795793
);
796-
} else if (window.podsDFVConfig) {
794+
} else if ( window.podsDFVConfig ) {
795+
const validationMessages = Object.keys(initialStoresWithValues[storeKey]).reduce((acc, key) => {
796+
acc[ key ] = [];
797+
return acc;
798+
}, {} );
797799
return initPodStore(
798800
window.podsDFVConfig,
799801
{
800-
...initialStoresWithValues[ storeKey ],
801-
validationMessages: [],
802+
...initialStoresWithValues[storeKey],
803+
validationMessages,
802804
needsValidation: false,
803805
},
804806
storeKey,

ui/js/dfv/src/store/selectors.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export const getPodOption = ( state, key ) => CURRENT_POD.getFrom( state )[ key
4444

4545
//Get all validation messages
4646
export const getValidationMessages = ( state ) => CURRENT_POD.getFrom( state ).validationMessages;
47-
export const getNeedsValidating = ( state ) => CURRENT_POD.getFrom( state ).needsValidating;
47+
export const getNeedsValidating = ( state ) => CURRENT_POD.getFrom( state ).needsValidation;
4848
//-- Pod Groups
4949
export const getGroups = ( state ) => GROUPS.getFrom( state );
5050

0 commit comments

Comments
 (0)