Skip to content

Commit

Permalink
feat: Add FieldError.connectedFormField connection (#424)
Browse files Browse the repository at this point in the history
* feat: Add `FieldError.connectedFormField` connection

* chore: add missing param to docblock
  • Loading branch information
justlevine authored Jun 12, 2024
1 parent 50e5cc2 commit c770c4e
Show file tree
Hide file tree
Showing 66 changed files with 722 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- fix!: Keep `PageField` with previous page data when filtering `formFields` by `pageNumber`. H/t @SamuelHadsall .
- fix: Handle RadioField submission values when using a custom "other" choice. H/t @Gytjarek .
- fix: Check for Submission Confirmation url before attempting to get the associated post ID.
- feat: Add `FieldError.connectedFormField` connection to `FieldError` type.
- dev: Use `FormFieldsDataLoader` to resolve fields instead of instantiating a new `Model`.
- chore: Add iterable type hints.
- chore!: Bump minimum WPGraphQL version to v1.26.0.
Expand Down
12 changes: 10 additions & 2 deletions docs/submitting-forms.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,12 @@ The `fieldValues` input takes an array of objects containing the `id` of the fie
url # The redirect URL - if the confirmation type is a "REDIRECT".
}
errors {
id # The field that failed validation.
id # The field ID that failed validation.
message
connectedFormField { # The full FormField object if you need more info.
database
type
}
}
entry {
# See docs on querying Entries.
Expand Down Expand Up @@ -150,7 +154,11 @@ If the field is NOT updated successfully, such as when a field validation error
"errors": [
{
"id": "1",
"message": "The text entered exceeds the maximum number of characters."
"message": "The text entered exceeds the maximum number of characters.",
"connectedFormField": {
"database": 1,
"type": "TEXT"
}
}
]
```
Expand Down
6 changes: 5 additions & 1 deletion docs/updating-entries.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ You can update a [Gravity Forms entry](https://docs.gravityforms.com/entry-objec
}
) {
errors {
id # The field that failed validation.
id # The field ID that failed validation.
message
connectedFormField { # The full FormField object if you need more info.
id
type
}
}
entry {
# See above section on querying Entries.
Expand Down
6 changes: 4 additions & 2 deletions src/Data/EntryObjectMutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,17 @@ public static function get_field_value_input( array $args, array $form, bool $is
* Generates array of field errors from the submission.
*
* @param array<int|string,string> $messages The Gravity Forms submission validation messages.
* @param int $form_id The ID of the form.
*
* @return array{message:string,id:int|string}[]
*/
public static function get_submission_errors( array $messages ): array {
public static function get_submission_errors( array $messages, int $form_id ): array {
return array_map(
static function ( $id, $message ): array {
static function ( $id, $message ) use ( $form_id ): array {
return [
'id' => $id,
'message' => $message,
'formId' => $form_id,
];
},
array_keys( $messages ),
Expand Down
2 changes: 1 addition & 1 deletion src/Mutation/SubmitDraftEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static function mutate_and_get_payload(): callable {
return [
'confirmation' => isset( $result['confirmation_type'] ) ? EntryObjectMutation::get_submission_confirmation( $result ) : null,
'entryId' => ! empty( $result['entry_id'] ) ? absint( $result['entry_id'] ) : null,
'errors' => isset( $result['validation_messages'] ) ? EntryObjectMutation::get_submission_errors( $result['validation_messages'] ) : null,
'errors' => isset( $result['validation_messages'] ) ? EntryObjectMutation::get_submission_errors( $result['validation_messages'], (int) $form['id'] ) : null,
];
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/Mutation/SubmitForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public static function mutate_and_get_payload(): callable {
return [
'confirmation' => isset( $submission['confirmation_type'] ) ? EntryObjectMutation::get_submission_confirmation( $submission ) : null,
'entryId' => ! empty( $submission['entry_id'] ) ? absint( $submission['entry_id'] ) : null,
'errors' => isset( $submission['validation_messages'] ) ? EntryObjectMutation::get_submission_errors( $submission['validation_messages'] ) : null,
'errors' => isset( $submission['validation_messages'] ) ? EntryObjectMutation::get_submission_errors( $submission['validation_messages'], $form_id ) : null,
'resumeToken' => $submission['resume_token'] ?? null,
'resumeUrl' => isset( $submission['resume_token'] ) ? GFUtils::get_resume_url( $submission['resume_token'], $entry_data['source_url'] ?? '', $form ) : null,
'submission' => $submission,
Expand Down
20 changes: 18 additions & 2 deletions src/Type/WPObject/FieldError.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

namespace WPGraphQL\GF\Type\WPObject;

use WPGraphQL\AppContext;
use WPGraphQL\GF\Data\Loader\FormFieldsLoader;
use WPGraphQL\GF\Type\WPInterface\FormField;
use WPGraphQL\GF\Type\WPObject\AbstractObject;

/**
Expand All @@ -36,14 +39,27 @@ public static function get_description(): string {
*/
public static function get_fields(): array {
return [
'id' => [
'id' => [
'type' => 'Float',
'description' => __( 'The field with the associated error message.', 'wp-graphql-gravity-forms' ),
],
'message' => [
'message' => [
'type' => 'String',
'description' => __( 'Error message.', 'wp-graphql-gravity-forms' ),
],
'connectedFormField' => [
'type' => FormField::$type,
'description' => __( 'The form field that the error is connected to.', 'wp-graphql-gravity-forms' ),
'resolve' => static function ( $source, array $args, AppContext $context ) {
if ( ! isset( $source['id'] ) || ! isset( $source['formId'] ) ) {
return null;
}

$id_for_loader = (string) $source['formId'] . ':' . (string) $source['id'];

return $context->get_loader( FormFieldsLoader::$name )->load_deferred( $id_for_loader );
},
],
];
}
}
12 changes: 12 additions & 0 deletions tests/wpunit/AddressFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ public function submit_form_mutation(): string {
errors {
id
message
connectedFormField {
databaseId
type
}
}
entry {
formFields {
Expand Down Expand Up @@ -215,6 +219,10 @@ public function update_entry_mutation(): string {
errors {
id
message
connectedFormField {
databaseId
type
}
}
entry {
formFields {
Expand Down Expand Up @@ -247,6 +255,10 @@ public function update_draft_entry_mutation(): string {
errors {
id
message
connectedFormField {
databaseId
type
}
}
entry: draftEntry {
formFields {
Expand Down
12 changes: 12 additions & 0 deletions tests/wpunit/CaptchaFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ public function submit_form_mutation(): string {
errors {
id
message
connectedFormField {
databaseId
type
}
}
entry {
formFields(where:{fieldTypes:TEXT}) {
Expand Down Expand Up @@ -213,6 +217,10 @@ public function update_entry_mutation(): string {
errors {
id
message
connectedFormField {
databaseId
type
}
}
entry {
formFields {
Expand All @@ -238,6 +246,10 @@ public function update_draft_entry_mutation(): string {
errors {
id
message
connectedFormField {
databaseId
type
}
}
entry: draftEntry {
formFields (where:{fieldTypes:TEXT}){
Expand Down
12 changes: 12 additions & 0 deletions tests/wpunit/ChainedSelectFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ public function submit_form_mutation(): string {
errors {
id
message
connectedFormField {
databaseId
type
}
}
entry {
formFields {
Expand Down Expand Up @@ -229,6 +233,10 @@ public function update_entry_mutation(): string {
errors {
id
message
connectedFormField {
databaseId
type
}
}
entry {
formFields {
Expand All @@ -254,6 +262,10 @@ public function update_draft_entry_mutation(): string {
errors {
id
message
connectedFormField {
databaseId
type
}
}
entry: draftEntry {
formFields {
Expand Down
12 changes: 12 additions & 0 deletions tests/wpunit/CheckboxFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,10 @@ public function submit_form_mutation(): string {
errors {
id
message
connectedFormField {
databaseId
type
}
}
entry {
formFields {
Expand Down Expand Up @@ -307,6 +311,10 @@ public function update_entry_mutation(): string {
errors {
id
message
connectedFormField {
databaseId
type
}
}
entry {
formFields {
Expand Down Expand Up @@ -336,6 +344,10 @@ public function update_draft_entry_mutation(): string {
errors {
id
message
connectedFormField {
databaseId
type
}
}
entry: draftEntry {
formFields {
Expand Down
12 changes: 12 additions & 0 deletions tests/wpunit/ConsentFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ public function submit_form_mutation(): string {
errors {
id
message
connectedFormField {
databaseId
type
}
}
entry {
formFields {
Expand Down Expand Up @@ -183,6 +187,10 @@ public function update_entry_mutation(): string {
errors {
id
message
connectedFormField {
databaseId
type
}
}
entry {
formFields {
Expand All @@ -208,6 +216,10 @@ public function update_draft_entry_mutation(): string {
errors {
id
message
connectedFormField {
databaseId
type
}
}
entry: draftEntry {
formFields {
Expand Down
12 changes: 12 additions & 0 deletions tests/wpunit/DateFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ public function submit_form_mutation(): string {
errors {
id
message
connectedFormField {
databaseId
type
}
}
entry {
formFields {
Expand Down Expand Up @@ -173,6 +177,10 @@ public function update_entry_mutation(): string {
errors {
id
message
connectedFormField {
databaseId
type
}
}
entry {
formFields {
Expand All @@ -198,6 +206,10 @@ public function update_draft_entry_mutation(): string {
errors {
id
message
connectedFormField {
databaseId
type
}
}
entry: draftEntry {
formFields {
Expand Down
12 changes: 12 additions & 0 deletions tests/wpunit/EmailFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ public function submit_form_mutation(): string {
errors {
id
message
connectedFormField {
databaseId
type
}
}
entry {
formFields {
Expand Down Expand Up @@ -217,6 +221,10 @@ public function update_entry_mutation(): string {
errors {
id
message
connectedFormField {
databaseId
type
}
}
entry {
formFields {
Expand All @@ -242,6 +250,10 @@ public function update_draft_entry_mutation(): string {
errors {
id
message
connectedFormField {
databaseId
type
}
}
entry: draftEntry {
formFields {
Expand Down
12 changes: 12 additions & 0 deletions tests/wpunit/EmailFieldWithConfirmationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ public function submit_form_mutation(): string {
errors {
id
message
connectedFormField {
databaseId
type
}
}
entry {
formFields {
Expand Down Expand Up @@ -222,6 +226,10 @@ public function update_entry_mutation(): string {
errors {
id
message
connectedFormField {
databaseId
type
}
}
entry {
formFields {
Expand All @@ -247,6 +255,10 @@ public function update_draft_entry_mutation(): string {
errors {
id
message
connectedFormField {
databaseId
type
}
}
entry: draftEntry {
formFields {
Expand Down
Loading

0 comments on commit c770c4e

Please sign in to comment.