Improve form submit error handling

This commit is contained in:
Tetrakern 2024-10-12 14:05:29 +02:00
parent ec901a971a
commit c5606836a4
4 changed files with 14 additions and 12 deletions

View File

@ -18,12 +18,12 @@ function fictioneer_ajax_submit_contact_form() {
// Emergency stop // Emergency stop
if ( get_option( 'fictioneer_disable_contact_forms' ) ) { if ( get_option( 'fictioneer_disable_contact_forms' ) ) {
wp_send_json_error( array( 'error' => _x( 'Contact forms have been disabled.', 'Contact form.', 'fictioneer' ) ) ); wp_send_json_error( array( 'failure' => _x( 'Contact forms have been disabled.', 'Contact form.', 'fictioneer' ) ) );
} }
// Validations // Validations
if ( empty( $_POST['message'] ) ) { if ( empty( $_POST['message'] ) ) {
wp_send_json_error( array( 'error' => _x( 'Message field empty.', 'Contact form.', 'fictioneer' ) ) ); wp_send_json_error( array( 'failure' => _x( 'Message field empty.', 'Contact form.', 'fictioneer' ) ) );
} }
if ( ! empty( $_POST['phone'] ) || filter_var( $_POST['terms'] ?? 0, FILTER_VALIDATE_BOOLEAN ) ) { if ( ! empty( $_POST['phone'] ) || filter_var( $_POST['terms'] ?? 0, FILTER_VALIDATE_BOOLEAN ) ) {
@ -36,11 +36,11 @@ function fictioneer_ajax_submit_contact_form() {
( ! empty( $_POST['name'] ) && $_POST['name'] != wp_strip_all_tags( $_POST['name'] ) ) ( ! empty( $_POST['name'] ) && $_POST['name'] != wp_strip_all_tags( $_POST['name'] ) )
) { ) {
// All fields are stripped of HTMl anyway, so this is only for the overly enthusiastic user. // All fields are stripped of HTMl anyway, so this is only for the overly enthusiastic user.
wp_send_json_error( array( 'error' => _x( 'Illegal HTML detected.', 'Contact form.', 'fictioneer' ) ) ); wp_send_json_error( array( 'failure' => _x( 'Illegal HTML detected.', 'Contact form.', 'fictioneer' ) ) );
} }
if ( ! empty( $_POST['email'] ) && ! is_email( $_POST['email'] ) ) { if ( ! empty( $_POST['email'] ) && ! is_email( $_POST['email'] ) ) {
wp_send_json_error( array( 'error' => _x( 'Invalid email address.', 'Contact form.', 'fictioneer' ) ) ); wp_send_json_error( array( 'failure' => _x( 'Invalid email address.', 'Contact form.', 'fictioneer' ) ) );
} }
if ( if (
@ -49,7 +49,7 @@ function fictioneer_ajax_submit_contact_form() {
) { ) {
// If the sender disables these fields, this is essentially acceptance // If the sender disables these fields, this is essentially acceptance
// since they went out of their way to manipulate the form. // since they went out of their way to manipulate the form.
wp_send_json_error( array( 'error' => _x( 'You need to accept the privacy policy.', 'Contact form.', 'fictioneer' ) ) ); wp_send_json_error( array( 'failure' => _x( 'You need to accept the privacy policy.', 'Contact form.', 'fictioneer' ) ) );
} }
// Setup // Setup
@ -185,10 +185,10 @@ function fictioneer_ajax_submit_contact_form() {
// someone his name or email address is blocked, etc. // someone his name or email address is blocked, etc.
if ( FICTIONEER_DISALLOWED_KEY_NOTICE && $offenders[0] && $offenders[1] ) { if ( FICTIONEER_DISALLOWED_KEY_NOTICE && $offenders[0] && $offenders[1] ) {
wp_send_json_error( wp_send_json_error(
array( 'error' => __( 'Disallowed key found: "' . implode( ', ', $offenders[1] ) . '".', 'fictioneer' ) ) array( 'failure' => __( 'Disallowed key found: "' . implode( ', ', $offenders[1] ) . '".', 'fictioneer' ) )
); );
} elseif ( $offenders[0] ) { } elseif ( $offenders[0] ) {
wp_send_json_error( array( 'error' => __( 'Disallowed keys found.', 'fictioneer' ) ) ); wp_send_json_error( array( 'failure' => __( 'Disallowed keys found.', 'fictioneer' ) ) );
} }
// Addresses // Addresses

File diff suppressed because one or more lines are too long

2
js/complete.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -1546,11 +1546,11 @@ function fcn_contactFormSubmit(button) {
form.querySelector('textarea').value = ''; form.querySelector('textarea').value = '';
button.innerHTML = button.dataset.done; button.innerHTML = button.dataset.done;
fcn_showNotification(response.data.success, 3, 'success'); fcn_showNotification(response.data.success, 3, 'success');
} else if (response.data.error) { } else if (response.data.failure) {
// Failure // Failure
button.disabled = false; button.disabled = false;
button.innerHTML = button.dataset.enabled; button.innerHTML = button.dataset.enabled;
fcn_showNotification(response.data.error, 5, 'warning'); fcn_showNotification(response.data.failure, 5, 'warning');
} }
}) })
.catch(error => { .catch(error => {
@ -1559,6 +1559,8 @@ function fcn_contactFormSubmit(button) {
button.disabled = false; button.disabled = false;
button.innerHTML = button.dataset.enabled; button.innerHTML = button.dataset.enabled;
} }
console.error('Error:', error);
}) })
.then(() => { .then(() => {
// Regardless of outcome // Regardless of outcome