| @@ -1,6 +1,5 @@ | ||
| 1 | 1 | <?php |
| 2 | -// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped, WordPress.Security.EscapeOutput.HeredocOutputNotEscaped, PluginCheck.CodeAnalysis.Heredoc -- PFBC Error class, HTML/JS output is controlled | |
| 3 | 2 | class AccuaForm_Error_Standard extends PFBCError { |
| 4 | 3 | protected $errorfound; |
| 5 | 4 | protected $errorsfound; |
| 6 | 5 | |
| @@ -15,48 +14,8 @@ | ||
| 15 | 14 | errorHTML += '\\x3Cli\\x3E' + response.errors[e] + '\\x3C/li\\x3E'; |
| 16 | 15 | } |
| 17 | 16 | errorHTML += '\\x3C/ul\\x3E\\x3C/div\\x3E'; |
| 18 | 17 | jQuery("#$id").append(errorHTML); |
| 19 | - | |
| 20 | -/* Apply ARIA attributes and inline errors for accessibility */ | |
| 21 | -if (response.elementErrors) { | |
| 22 | - jQuery.each(response.elementErrors, function(fieldName, fieldErrors) { | |
| 23 | - var field = jQuery('[name="' + fieldName + '"]', '#$id'); | |
| 24 | - if (!field.length) { | |
| 25 | - /* Elements with no named input in the DOM (e.g. reCAPTCHA v2) expose their name via data-accua-name */ | |
| 26 | - field = jQuery('[data-accua-name="' + fieldName + '"]', '#$id'); | |
| 27 | - } | |
| 28 | - if (field.length) { | |
| 29 | - var parent = field.closest('.pfbc-element, .pfbc-fieldwrap'); | |
| 30 | - var errorId = field.attr('id') + '-error'; | |
| 31 | - | |
| 32 | - /* Add ARIA attributes */ | |
| 33 | - field.attr('aria-invalid', 'true'); | |
| 34 | - field.attr('aria-describedby', errorId); | |
| 35 | - | |
| 36 | - /* Add error class to parent */ | |
| 37 | - parent.addClass('pfbc-element-has-error'); | |
| 38 | - | |
| 39 | - /* Remove existing inline error if any */ | |
| 40 | - parent.find('.pfbc-inline-error').remove(); | |
| 41 | - | |
| 42 | - /* Add inline error message */ | |
| 43 | - var inlineErrorHTML = '<div class="pfbc-inline-error" id="' + errorId + '" role="alert" aria-live="polite">'; | |
| 44 | - for (var i = 0; i < fieldErrors.length; i++) { | |
| 45 | - inlineErrorHTML += '<div class="pfbc-error-message">' + fieldErrors[i] + '</div>'; | |
| 46 | - } | |
| 47 | - inlineErrorHTML += '</div>'; | |
| 48 | - | |
| 49 | - /* For file inputs with help text, insert error after help text */ | |
| 50 | - var helpText = field.siblings('.pfbc-help').last(); | |
| 51 | - if (field.is('[type="file"]') && helpText.length) { | |
| 52 | - helpText.after(inlineErrorHTML); | |
| 53 | - } else { | |
| 54 | - field.after(inlineErrorHTML); | |
| 55 | - } | |
| 56 | - } | |
| 57 | - }); | |
| 58 | -} | |
| 59 | 18 | JS; |
| 60 | 19 | |
| 61 | 20 | } |
| 62 | 21 | |
| @@ -66,57 +25,14 @@ | ||
| 66 | 25 | var errorHTML = ''; |
| 67 | 26 | for(e = 0; e < errorSize; ++e) { |
| 68 | 27 | errorHTML += '\\x3Cli\\x3E' + response.errors[e] + '\\x3C/li\\x3E'; |
| 69 | 28 | } |
| 70 | - | |
| 71 | -/* Update summary area with server-side errors */ | |
| 72 | -if (typeof updateSummaryWithServerErrors === 'function') { | |
| 73 | - updateSummaryWithServerErrors(response.errors, response.elementErrors); | |
| 74 | -} | |
| 75 | - | |
| 76 | -/* Apply ARIA attributes and inline errors for accessibility */ | |
| 77 | -if (response.elementErrors) { | |
| 78 | - jQuery.each(response.elementErrors, function(fieldName, fieldErrors) { | |
| 79 | - var field = jQuery('[name="' + fieldName + '"]'); | |
| 80 | - if (!field.length) { | |
| 81 | - /* Elements with no named input in the DOM (e.g. reCAPTCHA v2) expose their name via data-accua-name */ | |
| 82 | - field = jQuery('[data-accua-name="' + fieldName + '"]'); | |
| 83 | - } | |
| 84 | - if (field.length) { | |
| 85 | - var parent = field.closest('.pfbc-element, .pfbc-fieldwrap'); | |
| 86 | - var errorId = field.attr('id') + '-error'; | |
| 87 | - | |
| 88 | - /* Add ARIA attributes */ | |
| 89 | - field.attr('aria-invalid', 'true'); | |
| 90 | - field.attr('aria-describedby', errorId); | |
| 91 | - | |
| 92 | - /* Add error class to parent */ | |
| 93 | - parent.addClass('pfbc-element-has-error'); | |
| 94 | - | |
| 95 | - /* Remove existing inline error if any */ | |
| 96 | - parent.find('.pfbc-inline-error').remove(); | |
| 97 | - | |
| 98 | - /* Add inline error message */ | |
| 99 | - var inlineErrorHTML = '<div class=\"pfbc-inline-error\" id=\"' + errorId + '\" role=\"alert\" aria-live=\"polite\">'; | |
| 100 | - for (var i = 0; i < fieldErrors.length; i++) { | |
| 101 | - inlineErrorHTML += '<div class=\"pfbc-error-message\">' + fieldErrors[i] + '</div>'; | |
| 102 | - } | |
| 103 | - inlineErrorHTML += '</div>'; | |
| 104 | - | |
| 105 | - /* For file inputs with help text, insert error after help text */ | |
| 106 | - var helpText = field.siblings('.pfbc-help').last(); | |
| 107 | - if (field.is('[type=\"file\"]') && helpText.length) { | |
| 108 | - helpText.after(inlineErrorHTML); | |
| 109 | - } else { | |
| 110 | - field.after(inlineErrorHTML); | |
| 111 | - } | |
| 112 | - } | |
| 113 | - }); | |
| 114 | -} | |
| 29 | +show_error_messages(errorHTML); | |
| 115 | 30 | JS; |
| 116 | 31 | |
| 117 | 32 | } |
| 118 | - public function __construct(?array $properties = null) { | |
| 33 | + | |
| 34 | + public function __construct(array $properties = null) { | |
| 119 | 35 | $this->errorfound = __('The following error was found:', 'contact-forms'); |
| 120 | 36 | $this->errorsfound = __('The following @errorsize errors were found:', 'contact-forms'); |
| 121 | 37 | $this->configure($properties); |
| 122 | 38 | } |
| @@ -153,24 +69,15 @@ | ||
| 153 | 69 | } |
| 154 | 70 | } |
| 155 | 71 | |
| 156 | 72 | public function getAjaxErrorResponse() { |
| 157 | - $flatErrors = $this->parse($this->form->getErrors()); | |
| 158 | - $elementErrors = $this->form->getErrors(); // Structured errors for ARIA attributes | |
| 159 | - return array( | |
| 160 | - 'errors' => $flatErrors, | |
| 161 | - 'elementErrors' => $elementErrors | |
| 162 | - ); | |
| 73 | + return $this->parse($this->form->getErrors()); | |
| 163 | 74 | } |
| 164 | 75 | |
| 165 | 76 | public function renderAjaxErrorResponse() { |
| 166 | - $flatErrors = $this->parse($this->form->getErrors()); | |
| 167 | - $elementErrors = $this->form->getErrors(); // Keep structured errors for field-level ARIA | |
| 168 | - if(!empty($flatErrors)) { | |
| 77 | + $errors = $this->parse($this->form->getErrors()); | |
| 78 | + if(!empty($errors)) { | |
| 169 | 79 | header("Content-type: application/json"); |
| 170 | - print _accua_forms_json_encode(array( | |
| 171 | - "errors" => $flatErrors, | |
| 172 | - "elementErrors" => $elementErrors // Add field-specific errors for ARIA attributes | |
| 173 | - )); | |
| 80 | + print _accua_forms_json_encode(array("errors" => $errors)); | |
| 174 | 81 | } |
| 175 | 82 | } |
| 176 | 83 | } |