-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Add support for elements validation in DataForm's array fields #71194
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
Changes from 6 commits
000595e
cf015c5
14e0308
59ade44
1db0a20
b7b321a
81d355f
b3b0c28
382cb4c
203af53
a65e3b0
538fc3a
8d5d551
0559f4c
b3b0024
fc42406
6d527cd
3f625d8
53c83fd
822e31c
d24449b
6617584
75a0a5f
fe03b3d
6ef2a57
1a94d66
f2e301d
27536bc
bc66855
300841d
08f9b87
a9872b2
32b0d52
5ed466c
20410e4
87c94f6
a4b1a3a
ce7465f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,14 @@ export function isItemValid< Item >( | |
| const isEmptyNullOrUndefined = ( value: any ) => | ||
| [ undefined, '', null ].includes( value ); | ||
|
|
||
| const isNotArrayEmptyOrContainsInvalidElements = ( value: any ) => { | ||
| return ( | ||
| ! Array.isArray( value ) || | ||
| value.length === 0 || | ||
| value.every( ( element: any ) => isEmptyNullOrUndefined( element ) ) | ||
| ); | ||
| }; | ||
|
|
||
| return _fields.every( ( field ) => { | ||
| const value = field.getValue( { item } ); | ||
|
|
||
|
|
@@ -34,6 +42,8 @@ export function isItemValid< Item >( | |
| ( field.type === 'email' && isEmptyNullOrUndefined( value ) ) || | ||
| ( field.type === 'integer' && | ||
| isEmptyNullOrUndefined( value ) ) || | ||
| ( field.type === 'array' && | ||
| isNotArrayEmptyOrContainsInvalidElements( value ) ) || | ||
| ( field.type === undefined && isEmptyNullOrUndefined( value ) ) | ||
| ) { | ||
| return false; | ||
|
|
@@ -44,6 +54,26 @@ export function isItemValid< Item >( | |
| } | ||
| } | ||
|
|
||
| if ( field.isValid.elements ) { | ||
| if ( field.elements ) { | ||
| const validValues = field.elements.map( | ||
| ( element ) => element.value | ||
| ); | ||
|
|
||
| if ( field.type === 'array' ) { | ||
| // For arrays, check if all values are valid elements | ||
| if ( Array.isArray( value ) ) { | ||
| return value.every( ( arrayItem ) => | ||
| validValues.includes( arrayItem ) | ||
| ); | ||
| } | ||
| return false; | ||
| } | ||
| // For single-value fields, check if the value is a valid element | ||
| return validValues.includes( value ); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In addition to this, we need to display what's happening to the user. Take the text control as an example. We handle
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
| } | ||
|
|
||
| if ( | ||
| typeof field.isValid.custom === 'function' && | ||
| field.isValid.custom( item, field ) !== null | ||
|
|
||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a small nit. For a second, I was confused for the use of "invalid elements" here, I thought why are we validating isValid.elements here if there's a specific check for it? Perhaps something along these lines makes that more clear?