Skip to content

Commit c770c4e

Browse files
authored
feat: Add FieldError.connectedFormField connection (#424)
* feat: Add `FieldError.connectedFormField` connection * chore: add missing param to docblock
1 parent 50e5cc2 commit c770c4e

File tree

66 files changed

+722
-11
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+722
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- fix!: Keep `PageField` with previous page data when filtering `formFields` by `pageNumber`. H/t @SamuelHadsall .
99
- fix: Handle RadioField submission values when using a custom "other" choice. H/t @Gytjarek .
1010
- fix: Check for Submission Confirmation url before attempting to get the associated post ID.
11+
- feat: Add `FieldError.connectedFormField` connection to `FieldError` type.
1112
- dev: Use `FormFieldsDataLoader` to resolve fields instead of instantiating a new `Model`.
1213
- chore: Add iterable type hints.
1314
- chore!: Bump minimum WPGraphQL version to v1.26.0.

docs/submitting-forms.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,12 @@ The `fieldValues` input takes an array of objects containing the `id` of the fie
121121
url # The redirect URL - if the confirmation type is a "REDIRECT".
122122
}
123123
errors {
124-
id # The field that failed validation.
124+
id # The field ID that failed validation.
125125
message
126+
connectedFormField { # The full FormField object if you need more info.
127+
database
128+
type
129+
}
126130
}
127131
entry {
128132
# See docs on querying Entries.
@@ -150,7 +154,11 @@ If the field is NOT updated successfully, such as when a field validation error
150154
"errors": [
151155
{
152156
"id": "1",
153-
"message": "The text entered exceeds the maximum number of characters."
157+
"message": "The text entered exceeds the maximum number of characters.",
158+
"connectedFormField": {
159+
"database": 1,
160+
"type": "TEXT"
161+
}
154162
}
155163
]
156164
```

docs/updating-entries.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@ You can update a [Gravity Forms entry](https://docs.gravityforms.com/entry-objec
2727
}
2828
) {
2929
errors {
30-
id # The field that failed validation.
30+
id # The field ID that failed validation.
3131
message
32+
connectedFormField { # The full FormField object if you need more info.
33+
id
34+
type
35+
}
3236
}
3337
entry {
3438
# See above section on querying Entries.

src/Data/EntryObjectMutation.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,17 @@ public static function get_field_value_input( array $args, array $form, bool $is
116116
* Generates array of field errors from the submission.
117117
*
118118
* @param array<int|string,string> $messages The Gravity Forms submission validation messages.
119+
* @param int $form_id The ID of the form.
119120
*
120121
* @return array{message:string,id:int|string}[]
121122
*/
122-
public static function get_submission_errors( array $messages ): array {
123+
public static function get_submission_errors( array $messages, int $form_id ): array {
123124
return array_map(
124-
static function ( $id, $message ): array {
125+
static function ( $id, $message ) use ( $form_id ): array {
125126
return [
126127
'id' => $id,
127128
'message' => $message,
129+
'formId' => $form_id,
128130
];
129131
},
130132
array_keys( $messages ),

src/Mutation/SubmitDraftEntry.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public static function mutate_and_get_payload(): callable {
106106
return [
107107
'confirmation' => isset( $result['confirmation_type'] ) ? EntryObjectMutation::get_submission_confirmation( $result ) : null,
108108
'entryId' => ! empty( $result['entry_id'] ) ? absint( $result['entry_id'] ) : null,
109-
'errors' => isset( $result['validation_messages'] ) ? EntryObjectMutation::get_submission_errors( $result['validation_messages'] ) : null,
109+
'errors' => isset( $result['validation_messages'] ) ? EntryObjectMutation::get_submission_errors( $result['validation_messages'], (int) $form['id'] ) : null,
110110
];
111111
};
112112
}

src/Mutation/SubmitForm.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public static function mutate_and_get_payload(): callable {
165165
return [
166166
'confirmation' => isset( $submission['confirmation_type'] ) ? EntryObjectMutation::get_submission_confirmation( $submission ) : null,
167167
'entryId' => ! empty( $submission['entry_id'] ) ? absint( $submission['entry_id'] ) : null,
168-
'errors' => isset( $submission['validation_messages'] ) ? EntryObjectMutation::get_submission_errors( $submission['validation_messages'] ) : null,
168+
'errors' => isset( $submission['validation_messages'] ) ? EntryObjectMutation::get_submission_errors( $submission['validation_messages'], $form_id ) : null,
169169
'resumeToken' => $submission['resume_token'] ?? null,
170170
'resumeUrl' => isset( $submission['resume_token'] ) ? GFUtils::get_resume_url( $submission['resume_token'], $entry_data['source_url'] ?? '', $form ) : null,
171171
'submission' => $submission,

src/Type/WPObject/FieldError.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
namespace WPGraphQL\GF\Type\WPObject;
1313

14+
use WPGraphQL\AppContext;
15+
use WPGraphQL\GF\Data\Loader\FormFieldsLoader;
16+
use WPGraphQL\GF\Type\WPInterface\FormField;
1417
use WPGraphQL\GF\Type\WPObject\AbstractObject;
1518

1619
/**
@@ -36,14 +39,27 @@ public static function get_description(): string {
3639
*/
3740
public static function get_fields(): array {
3841
return [
39-
'id' => [
42+
'id' => [
4043
'type' => 'Float',
4144
'description' => __( 'The field with the associated error message.', 'wp-graphql-gravity-forms' ),
4245
],
43-
'message' => [
46+
'message' => [
4447
'type' => 'String',
4548
'description' => __( 'Error message.', 'wp-graphql-gravity-forms' ),
4649
],
50+
'connectedFormField' => [
51+
'type' => FormField::$type,
52+
'description' => __( 'The form field that the error is connected to.', 'wp-graphql-gravity-forms' ),
53+
'resolve' => static function ( $source, array $args, AppContext $context ) {
54+
if ( ! isset( $source['id'] ) || ! isset( $source['formId'] ) ) {
55+
return null;
56+
}
57+
58+
$id_for_loader = (string) $source['formId'] . ':' . (string) $source['id'];
59+
60+
return $context->get_loader( FormFieldsLoader::$name )->load_deferred( $id_for_loader );
61+
},
62+
],
4763
];
4864
}
4965
}

tests/wpunit/AddressFieldTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ public function submit_form_mutation(): string {
177177
errors {
178178
id
179179
message
180+
connectedFormField {
181+
databaseId
182+
type
183+
}
180184
}
181185
entry {
182186
formFields {
@@ -215,6 +219,10 @@ public function update_entry_mutation(): string {
215219
errors {
216220
id
217221
message
222+
connectedFormField {
223+
databaseId
224+
type
225+
}
218226
}
219227
entry {
220228
formFields {
@@ -247,6 +255,10 @@ public function update_draft_entry_mutation(): string {
247255
errors {
248256
id
249257
message
258+
connectedFormField {
259+
databaseId
260+
type
261+
}
250262
}
251263
entry: draftEntry {
252264
formFields {

tests/wpunit/CaptchaFieldTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ public function submit_form_mutation(): string {
182182
errors {
183183
id
184184
message
185+
connectedFormField {
186+
databaseId
187+
type
188+
}
185189
}
186190
entry {
187191
formFields(where:{fieldTypes:TEXT}) {
@@ -213,6 +217,10 @@ public function update_entry_mutation(): string {
213217
errors {
214218
id
215219
message
220+
connectedFormField {
221+
databaseId
222+
type
223+
}
216224
}
217225
entry {
218226
formFields {
@@ -238,6 +246,10 @@ public function update_draft_entry_mutation(): string {
238246
errors {
239247
id
240248
message
249+
connectedFormField {
250+
databaseId
251+
type
252+
}
241253
}
242254
entry: draftEntry {
243255
formFields (where:{fieldTypes:TEXT}){

tests/wpunit/ChainedSelectFieldTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ public function submit_form_mutation(): string {
198198
errors {
199199
id
200200
message
201+
connectedFormField {
202+
databaseId
203+
type
204+
}
201205
}
202206
entry {
203207
formFields {
@@ -229,6 +233,10 @@ public function update_entry_mutation(): string {
229233
errors {
230234
id
231235
message
236+
connectedFormField {
237+
databaseId
238+
type
239+
}
232240
}
233241
entry {
234242
formFields {
@@ -254,6 +262,10 @@ public function update_draft_entry_mutation(): string {
254262
errors {
255263
id
256264
message
265+
connectedFormField {
266+
databaseId
267+
type
268+
}
257269
}
258270
entry: draftEntry {
259271
formFields {

0 commit comments

Comments
 (0)