Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add checkValidation and isValid methods to window.podsDFV #7064

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
6d41902
Add checkValidation and isValid methods #6898
Shelob9 Apr 20, 2023
4397eb3
Auto rebuild assets
Shelob9 Apr 20, 2023
bc64340
#689 start adding validation messages to store
Shelob9 Apr 20, 2023
10a8b8c
Auto rebuild assets
Shelob9 Apr 20, 2023
6b7a444
#6898 make new validation state work with reducer/init state
Shelob9 Apr 21, 2023
bb82036
Auto rebuild assets
Shelob9 Apr 21, 2023
0f090ff
Set field-specific validation messsage in store #6898
Shelob9 Apr 21, 2023
4357886
Put in needsValidating toggle
Shelob9 Apr 21, 2023
d68724d
Auto rebuild assets
Shelob9 Apr 21, 2023
d8f14fe
reformat dfv
Shelob9 Apr 21, 2023
f73d584
Dispatch in checkValidation #6898
Shelob9 Apr 21, 2023
eabb5f2
Auto rebuild assets
Shelob9 Apr 21, 2023
eb520c8
Abstract pod/itemId/formCounter logic and handle undefined cases
sc0ttkclark Apr 25, 2023
8e6286d
Auto rebuild assets
sc0ttkclark Apr 25, 2023
ab3ced8
Return true or false correctly from formIsValid #6898
Shelob9 Apr 25, 2023
07576ea
Use message as key in ValidationMessages to avoid duplicate keys #6898
Shelob9 Apr 25, 2023
0e4e052
Update ui/js/dfv/src/pods-dfv.js
Shelob9 Apr 25, 2023
92bc100
Fix duplicate storeKey prop and rebuild
sc0ttkclark Apr 25, 2023
7d59e27
Fix for email validation always showing as invalid
sc0ttkclark Apr 25, 2023
b44d327
Auto rebuild assets
sc0ttkclark Apr 25, 2023
f651e06
initialize validation state for pods editor #6898
Shelob9 Apr 25, 2023
a1a10e3
Auto rebuild assets
Shelob9 Apr 25, 2023
fd5e633
pass storeKey down from RepeatableFieldList to SubFieldWrapper
Shelob9 Jul 10, 2023
dc6a448
Only return messages if there are any, prevent error
Shelob9 Jul 14, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ui/js/dfv/src/components/connected-field-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const ConnectedFieldWrapper = compose( [
return {
...valueData,
allPodValues,
storeKey,
};
} ),
withDispatch( ( storeDispatch, ownProps ) => {
Expand Down
5 changes: 4 additions & 1 deletion ui/js/dfv/src/components/field-wrapper/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const FieldWrapper = ( props ) => {
values,
setOptionValue,
allPodValues,
storeKey,
} = props;

const {
Expand Down Expand Up @@ -139,7 +140,9 @@ export const FieldWrapper = ( props ) => {
condition: () => true === toBool( required ),
},
],
value
value,
name,
storeKey
);

// Don't render a field that hasn't had its dependencies met.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ const SubfieldWrapper = ( {
// Subfields get their own set of validation rules
const [ validationMessages, addValidationRules ] = useValidation(
[],
value
value,
subfieldConfig.name
Shelob9 marked this conversation as resolved.
Show resolved Hide resolved
);

// Set up useSortable hook
Expand Down
2 changes: 1 addition & 1 deletion ui/js/dfv/src/components/validation-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const ValidationMessages = ( { messages } ) => {
<div className="pods-validation-messages">
{ messages.map( ( message ) => (
<Notice
key="message"
key={ message }
status="error"
isDismissible={ false }
politeness="polite"
Expand Down
34 changes: 28 additions & 6 deletions ui/js/dfv/src/hooks/useValidation.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
import { useState, useEffect } from 'react';
import {
useSelect,
useDispatch,
} from '@wordpress/data';

const useValidation = ( defaultRules = [], value ) => {
const useValidation = ( defaultRules = [], value, fieldName, storeKey ) => {
const [ validationRules, setValidationRules ] = useState( defaultRules );
const [ validationMessages, setValidationMessages ] = useState( [] );

//Validation messages for this field
const validationMessages = useSelect( ( select ) => {
Shelob9 marked this conversation as resolved.
Show resolved Hide resolved
const currentMessages = select( storeKey ).getValidationMessages();
//Return for this field
if ( currentMessages.hasOwnProperty( fieldName ) ) {
return currentMessages[ fieldName ];
}
return [];
}, [ fieldName ] );

//Set validation messages for this field
const { setValidationMessages } = useDispatch( storeKey );
const needsValidation = useSelect( ( select ) => {
Shelob9 marked this conversation as resolved.
Show resolved Hide resolved
return select( storeKey ).getNeedsValidating();
}, [] );

const toggleNeedsValidating = useDispatch( storeKey ).toggleNeedsValidating();

useEffect( () => {
const newMessages = [];
Expand All @@ -20,9 +41,11 @@ const useValidation = ( defaultRules = [], value ) => {
}
}
} );

setValidationMessages( newMessages );
}, [ value ] );
setValidationMessages( fieldName, newMessages );
if ( needsValidation ) {
toggleNeedsValidating();
}
}, [ value, validationRules, needsValidation, toggleNeedsValidating, setValidationMessages ] );

const addValidationRules = ( rules = [] ) => {
rules.forEach( ( rule ) => {
Expand All @@ -34,7 +57,6 @@ const useValidation = ( defaultRules = [], value ) => {
} );
} );
};

return [
validationMessages,
addValidationRules,
Expand Down
Loading