diff --git a/classes/controllers/FrmEntriesAJAXSubmitController.php b/classes/controllers/FrmEntriesAJAXSubmitController.php index 21286af743..3ce89a3f18 100644 --- a/classes/controllers/FrmEntriesAJAXSubmitController.php +++ b/classes/controllers/FrmEntriesAJAXSubmitController.php @@ -85,6 +85,7 @@ public static function ajax_create() { 'form' => $form, 'entry_id' => 0, 'class' => FrmFormsHelper::form_error_class(), + 'role' => 'alert', ) ); } else { diff --git a/classes/helpers/FrmFormsHelper.php b/classes/helpers/FrmFormsHelper.php index 0499d44384..c6e982cda2 100644 --- a/classes/helpers/FrmFormsHelper.php +++ b/classes/helpers/FrmFormsHelper.php @@ -288,15 +288,14 @@ public static function get_invalid_error_message( $args ) { $settings_args['current_form'] = $args['form']->id; } - $frm_settings = FrmAppHelper::get_settings( $settings_args ); - + $frm_settings = FrmAppHelper::get_settings( $settings_args ); $field_error_messages = self::get_clickable_field_error_messages( $args ); - - $invalid_msg = '' . do_shortcode( $frm_settings->invalid_msg ) . ''; + $invalid_msg = '' . do_shortcode( $frm_settings->invalid_msg ) . ''; if ( $field_error_messages ) { $invalid_msg .= ""; } + return apply_filters( 'frm_invalid_error_message', $invalid_msg, $args ); } @@ -310,30 +309,20 @@ public static function get_invalid_error_message( $args ) { * @return string */ private static function get_clickable_field_error_messages( $args ) { - $field_error_messages = ''; - if ( empty( $args['errors'] ) ) { - return $field_error_messages; + return ''; } - $field_ids = array_map( - function ( $field_plus_id ) { - $field_id = str_replace( 'field', '', $field_plus_id ); - - if ( strpos( $field_id, '-' ) !== false ) { - $field_id = explode( '-', $field_id )[0]; - } - return $field_id; - }, - array_keys( $args['errors'] ) - ); - $field_keys = FrmDb::get_results( 'frm_fields', array( 'id' => $field_ids ), 'id,field_key,type' ); + // Parse each error key once into its field ID and repeater row suffix, skipping + // non-field errors like 'form' or 'spam' that have no input to link to. + $parsed_errors = array(); + $field_ids = array(); foreach ( $args['errors'] as $field_plus_id => $error ) { - $field_id = str_replace( 'field', '', $field_plus_id ); + $field_id = preg_replace( '/^field/', '', $field_plus_id ); $row = ''; - if ( strpos( $field_id, '-' ) !== false ) { + if ( str_contains( $field_id, '-' ) ) { $field_id_parts = explode( '-', $field_id ); if ( count( $field_id_parts ) === 3 ) { @@ -342,24 +331,90 @@ function ( $field_plus_id ) { } } - $index = array_search( $field_id, array_column( $field_keys, 'id' ), true ); + if ( ! is_numeric( $field_id ) ) { + continue; + } + + $field_ids[] = (int) $field_id; + $parsed_errors[] = compact( 'field_id', 'row', 'error' ); + } + + if ( ! $field_ids ) { + return ''; + } + + $fields = FrmDb::get_results( 'frm_fields', array( 'id' => $field_ids ), 'id,field_key,type,field_order,form_id' ); + $fields_by_id = array(); + + foreach ( $fields as $field ) { + $fields_by_id[ (int) $field->id ] = $field; + } + + // Error messages are admin configured and may include shortcodes, so allow the same + // inline formatting Formidable permits elsewhere while stripping anything unsafe. Anchors + // are intentionally excluded so a message link cannot nest inside the summary link. + $allowed_tags = array( 'strong', 'b', 'em', 'i', 'u', 'span', 'code', 'br', 'sub', 'sup', 'mark', 'small' ); + $field_error_messages = ''; + + foreach ( $parsed_errors as $parsed_error ) { + $field_id = (int) $parsed_error['field_id']; - if ( false === $index ) { + if ( ! isset( $fields_by_id[ $field_id ] ) ) { continue; } - $html_id = 'field_' . $field_keys[ $index ]->field_key . $row; + $field = $fields_by_id[ $field_id ]; + $error = FrmAppHelper::kses( $parsed_error['error'], $allowed_tags ); - if ( in_array( $field_keys[ $index ]->type, array( 'checkbox', 'radio' ), true ) ) { - // Needed to focus on the first option when error link is clicked. + if ( ! self::error_field_is_linkable( $field ) ) { + // The field has no focusable input on the page being shown (a hidden field, or a + // field on another page of a multi-page form), so list the error as plain text + // rather than a link that would go nowhere when clicked. + $field_error_messages .= '
  • ' . $error . '
  • '; + continue; + } + + $html_id = 'field_' . $field->field_key . $parsed_error['row']; + + if ( in_array( $field->type, array( 'checkbox', 'radio' ), true ) ) { + // Needed to focus on the first option when the error link is clicked. $html_id .= '-0'; } - $field_error_messages .= '
  • ' . $error . '
  • '; + $field_error_messages .= '
  • ' . $error . '
  • '; }//end foreach + return $field_error_messages; } + /** + * Whether an error summary should link to the field, or just list its message as plain text. + * + * A link is only useful when the field renders a focusable input that is actually on the page + * being shown. Hidden and user ID fields render as hidden inputs that cannot receive focus, and + * a field on another page of a multi-page form is not visible, so neither should be linked. + * + * @since x.x + * + * @param stdClass $field Field row with at least type, field_order and form_id. + * + * @return bool + */ + private static function error_field_is_linkable( $field ) { + if ( in_array( $field->type, array( 'hidden', 'user_id' ), true ) ) { + // Hidden inputs cannot receive focus, so there is nothing to link to. + return false; + } + + if ( ! is_callable( 'FrmProFieldsHelper::field_on_current_page' ) ) { + // Multi-page forms are a Pro feature; in Lite every field is on the only page. + return true; + } + + // On a multi-page form, do not link a field that is on a page other than the one being shown. + return FrmProFieldsHelper::field_on_current_page( $field ); + } + /** * @param array $atts { * The success message details. @@ -368,6 +423,9 @@ function ( $field_plus_id ) { * @type stdClass $form * @type int $entry_id * @type string $class + * @type string $role Optional. ARIA live region role for the wrapper. Defaults to 'status'. + * Pass 'alert' when the message reports a validation error so it is + * announced assertively, matching the non-ajax error wrapper. * } * * @return string @@ -395,7 +453,9 @@ public static function get_success_message( $atts ) { } $message = do_shortcode( $message ); - return '
    ' . $message . '
    '; + $role = $atts['role'] ?? 'status'; + + return '
    ' . $message . '
    '; } /** diff --git a/css/_single_theme.css.php b/css/_single_theme.css.php index 90ed2bf442..0262af134f 100644 --- a/css/_single_theme.css.php +++ b/css/_single_theme.css.php @@ -423,20 +423,21 @@ } . .frm_error_style span{ - font-weight: bold;; + font-weight: bold; } . .frm_error_style ul{ - list-style: inside;; - color: ; + list-style: inside; + color: var(--error-text); + margin-bottom: 0; } . .frm_error_style ul li a{ - color: ; + color: var(--error-text); } . .frm_error_style ul li a:hover{ - text-decoration: underline;; + text-decoration: underline; } diff --git a/stubs.php b/stubs.php index d2fbe140c7..7edadbe48d 100644 --- a/stubs.php +++ b/stubs.php @@ -316,6 +316,13 @@ public static function replace_non_standard_formidable_shortcodes( $args, &$valu */ public static function is_field_visible_to_user( $field ) { } + /** + * @param array|int|object $field + * + * @return bool + */ + public static function field_on_current_page( $field ) { + } } class FrmViewsAppHelper { /** diff --git a/tests/phpunit/forms/test_FrmFormsHelper.php b/tests/phpunit/forms/test_FrmFormsHelper.php index 41a294d59d..f9bdb81661 100644 --- a/tests/phpunit/forms/test_FrmFormsHelper.php +++ b/tests/phpunit/forms/test_FrmFormsHelper.php @@ -146,4 +146,210 @@ private function create_form_with_custom_style_value( $custom_style ) { ) ); } + + /** + * The invalid error message should include a list of links that jump to each field that failed validation. + * + * @covers FrmFormsHelper::get_invalid_error_message + */ + public function test_get_invalid_error_message_builds_clickable_field_links() { + $this->form = $this->factory->form->create_and_get(); + + $text_id = $this->create_field_with_key( 'text', 'my_text' ); + $select_id = $this->create_field_with_key( 'select', 'my_select' ); + $checkbox_id = $this->create_field_with_key( 'checkbox', 'my_checkbox' ); + $radio_id = $this->create_field_with_key( 'radio', 'my_radio' ); + + $message = FrmFormsHelper::get_invalid_error_message( + array( + 'form' => $this->form, + 'errors' => array( + 'field' . $text_id => 'Text is required', + 'field' . $select_id => 'Select is required', + 'field' . $checkbox_id => 'Checkbox is required', + 'field' . $radio_id => 'Radio is required', + ), + ) + ); + + // The base invalid message is wrapped in a span, and the links are inside a list. + $this->assertStringContainsString( '