| 1 |
<?php |
| 2 |
/** |
| 3 |
* PFBC (PHP Form Builder Class) - Third-party library |
| 4 |
* @package PFBC |
| 5 |
*/ |
| 6 |
|
| 7 |
// phpcs:disable WordPress.Security.EscapeOutput, WordPress.NamingConventions.PrefixAllGlobals, PluginCheck.CodeAnalysis.Heredoc -- Third-party library |
| 8 |
|
| 9 |
class Error_Standard extends PFBCError { |
| 10 |
public function applyAjaxErrorResponse() { |
| 11 |
$id = $this->form->getId(); |
| 12 |
echo <<<JS |
| 13 |
var errorSize = response.errors.length; |
| 14 |
if(errorSize == 1) |
| 15 |
var errorFormat = "error was"; |
| 16 |
else |
| 17 |
var errorFormat = errorSize + " errors were"; |
| 18 |
|
| 19 |
var errorHTML = '<div class="pfbc-error ui-state-error ui-corner-all">The following ' + errorFormat + ' found:<ul>'; |
| 20 |
for(e = 0; e < errorSize; ++e) |
| 21 |
errorHTML += '<li>' + response.errors[e] + '</li>'; |
| 22 |
errorHTML += '</ul></div>'; |
| 23 |
jQuery("#$id").prepend(errorHTML); |
| 24 |
|
| 25 |
/* Apply ARIA attributes and inline errors for accessibility */ |
| 26 |
if (response.elementErrors) { |
| 27 |
jQuery.each(response.elementErrors, function(fieldName, fieldErrors) { |
| 28 |
var field = jQuery('[name="' + fieldName + '"]', '#$id'); |
| 29 |
if (field.length) { |
| 30 |
var parent = field.closest('.pfbc-element, .pfbc-fieldwrap'); |
| 31 |
var errorId = field.attr('id') + '-error'; |
| 32 |
|
| 33 |
/* Add ARIA attributes */ |
| 34 |
field.attr('aria-invalid', 'true'); |
| 35 |
field.attr('aria-describedby', errorId); |
| 36 |
|
| 37 |
/* Add error class to parent */ |
| 38 |
parent.addClass('pfbc-element-has-error'); |
| 39 |
|
| 40 |
/* Remove existing inline error if any */ |
| 41 |
parent.find('.pfbc-inline-error').remove(); |
| 42 |
|
| 43 |
/* Add inline error message */ |
| 44 |
var inlineErrorHTML = '<div class="pfbc-inline-error" id="' + errorId + '" role="alert" aria-live="polite">'; |
| 45 |
for (var i = 0; i < fieldErrors.length; i++) { |
| 46 |
inlineErrorHTML += '<div class="pfbc-error-message">' + fieldErrors[i] + '</div>'; |
| 47 |
} |
| 48 |
inlineErrorHTML += '</div>'; |
| 49 |
field.after(inlineErrorHTML); |
| 50 |
} |
| 51 |
}); |
| 52 |
} |
| 53 |
JS; |
| 54 |
|
| 55 |
} |
| 56 |
|
| 57 |
private function parse($errors) { |
| 58 |
$list = array(); |
| 59 |
if(!empty($errors)) { |
| 60 |
$keys = array_keys($errors); |
| 61 |
$keySize = sizeof($keys); |
| 62 |
for($k = 0; $k < $keySize; ++$k) |
| 63 |
$list = array_merge($list, $errors[$keys[$k]]); |
| 64 |
} |
| 65 |
return $list; |
| 66 |
} |
| 67 |
|
| 68 |
public function render() { |
| 69 |
$errors = $this->parse($this->form->getErrors()); |
| 70 |
if(!empty($errors)) { |
| 71 |
$size = sizeof($errors); |
| 72 |
if($size == 1) |
| 73 |
$format = "error was"; |
| 74 |
else |
| 75 |
$format = $size . " errors were"; |
| 76 |
|
| 77 |
echo '<div class="pfbc-error ui-state-error ui-corner-all" role="alert" aria-live="assertive" tabindex="-1">'; |
| 78 |
echo '<strong class="pfbc-error-heading">The following ', $format, ' found:</strong>'; |
| 79 |
echo '<ul class="pfbc-error-list"><li>', implode("</li><li>", $errors), "</li></ul>"; |
| 80 |
echo '</div>'; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
public function renderAjaxErrorResponse() { |
| 85 |
$flatErrors = $this->parse($this->form->getErrors()); |
| 86 |
$elementErrors = $this->form->getErrors(); // Keep structured errors for field-level ARIA |
| 87 |
if(!empty($flatErrors)) { |
| 88 |
header("Content-type: application/json"); |
| 89 |
print _accua_forms_json_encode(array( |
| 90 |
"errors" => $flatErrors, |
| 91 |
"elementErrors" => $elementErrors // Add field-specific errors for ARIA attributes |
| 92 |
)); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|