| 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 |
class AccuaForm_Error_Standard extends PFBCError { |
| 4 |
protected $errorfound; |
| 5 |
protected $errorsfound; |
| 6 |
|
| 7 |
public function applyAjaxErrorResponse() { |
| 8 |
$id = $this->form->getId(); |
| 9 |
//$errorfound = _accua_forms_json_encode($this->errorfound); |
| 10 |
//$errorsfound = _accua_forms_json_encode($this->errorsfound); |
| 11 |
echo <<<JS |
| 12 |
var errorSize = response.errors.length; |
| 13 |
var errorHTML = '\\x3Cdiv class="pfbc-error ui-state-error ui-corner-all"\\x3E\\x3Cul\\x3E'; |
| 14 |
for(e = 0; e < errorSize; ++e) { |
| 15 |
errorHTML += '\\x3Cli\\x3E' + response.errors[e] + '\\x3C/li\\x3E'; |
| 16 |
} |
| 17 |
errorHTML += '\\x3C/ul\\x3E\\x3C/div\\x3E'; |
| 18 |
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 |
var parent = field.closest('.pfbc-element, .pfbc-fieldwrap'); |
| 26 |
var errorId = field.attr('id') + '-error'; |
| 27 |
|
| 28 |
/* Add ARIA attributes */ |
| 29 |
field.attr('aria-invalid', 'true'); |
| 30 |
field.attr('aria-describedby', errorId); |
| 31 |
|
| 32 |
/* Add error class to parent */ |
| 33 |
parent.addClass('pfbc-element-has-error'); |
| 34 |
|
| 35 |
/* Remove existing inline error if any */ |
| 36 |
parent.find('.pfbc-inline-error').remove(); |
| 37 |
|
| 38 |
/* Add inline error message */ |
| 39 |
var inlineErrorHTML = '<div class="pfbc-inline-error" id="' + errorId + '" role="alert" aria-live="polite">'; |
| 40 |
for (var i = 0; i < fieldErrors.length; i++) { |
| 41 |
inlineErrorHTML += '<div class="pfbc-error-message">' + fieldErrors[i] + '</div>'; |
| 42 |
} |
| 43 |
inlineErrorHTML += '</div>'; |
| 44 |
|
| 45 |
/* For file inputs with help text, insert error after help text */ |
| 46 |
var helpText = field.siblings('.pfbc-help').last(); |
| 47 |
if (field.is('[type="file"]') && helpText.length) { |
| 48 |
helpText.after(inlineErrorHTML); |
| 49 |
} else { |
| 50 |
field.after(inlineErrorHTML); |
| 51 |
} |
| 52 |
} |
| 53 |
}); |
| 54 |
} |
| 55 |
JS; |
| 56 |
|
| 57 |
} |
| 58 |
|
| 59 |
public function applyAjaxErrorResponseUsingShowErrorMessages() { |
| 60 |
echo <<<JS |
| 61 |
var errorSize = response.errors.length; |
| 62 |
var errorHTML = ''; |
| 63 |
for(e = 0; e < errorSize; ++e) { |
| 64 |
errorHTML += '\\x3Cli\\x3E' + response.errors[e] + '\\x3C/li\\x3E'; |
| 65 |
} |
| 66 |
|
| 67 |
/* Update summary area with server-side errors */ |
| 68 |
if (typeof updateSummaryWithServerErrors === 'function') { |
| 69 |
updateSummaryWithServerErrors(response.errors, response.elementErrors); |
| 70 |
} |
| 71 |
|
| 72 |
/* Apply ARIA attributes and inline errors for accessibility */ |
| 73 |
if (response.elementErrors) { |
| 74 |
jQuery.each(response.elementErrors, function(fieldName, fieldErrors) { |
| 75 |
var field = jQuery('[name="' + fieldName + '"]'); |
| 76 |
if (field.length) { |
| 77 |
var parent = field.closest('.pfbc-element, .pfbc-fieldwrap'); |
| 78 |
var errorId = field.attr('id') + '-error'; |
| 79 |
|
| 80 |
/* Add ARIA attributes */ |
| 81 |
field.attr('aria-invalid', 'true'); |
| 82 |
field.attr('aria-describedby', errorId); |
| 83 |
|
| 84 |
/* Add error class to parent */ |
| 85 |
parent.addClass('pfbc-element-has-error'); |
| 86 |
|
| 87 |
/* Remove existing inline error if any */ |
| 88 |
parent.find('.pfbc-inline-error').remove(); |
| 89 |
|
| 90 |
/* Add inline error message */ |
| 91 |
var inlineErrorHTML = '<div class=\"pfbc-inline-error\" id=\"' + errorId + '\" role=\"alert\" aria-live=\"polite\">'; |
| 92 |
for (var i = 0; i < fieldErrors.length; i++) { |
| 93 |
inlineErrorHTML += '<div class=\"pfbc-error-message\">' + fieldErrors[i] + '</div>'; |
| 94 |
} |
| 95 |
inlineErrorHTML += '</div>'; |
| 96 |
|
| 97 |
/* For file inputs with help text, insert error after help text */ |
| 98 |
var helpText = field.siblings('.pfbc-help').last(); |
| 99 |
if (field.is('[type=\"file\"]') && helpText.length) { |
| 100 |
helpText.after(inlineErrorHTML); |
| 101 |
} else { |
| 102 |
field.after(inlineErrorHTML); |
| 103 |
} |
| 104 |
} |
| 105 |
}); |
| 106 |
} |
| 107 |
JS; |
| 108 |
|
| 109 |
} |
| 110 |
public function __construct(?array $properties = null) { |
| 111 |
$this->errorfound = __('The following error was found:', 'contact-forms'); |
| 112 |
$this->errorsfound = __('The following @errorsize errors were found:', 'contact-forms'); |
| 113 |
$this->configure($properties); |
| 114 |
} |
| 115 |
|
| 116 |
public function setErrorsFound($text = '') { |
| 117 |
$this->errorsfound = $text; |
| 118 |
} |
| 119 |
|
| 120 |
public function setErrorFound($text = '') { |
| 121 |
$this->errorfound = $text; |
| 122 |
} |
| 123 |
|
| 124 |
private function parse($errors) { |
| 125 |
$list = array(); |
| 126 |
if(!empty($errors)) { |
| 127 |
$keys = array_keys($errors); |
| 128 |
$keySize = sizeof($keys); |
| 129 |
for($k = 0; $k < $keySize; ++$k) |
| 130 |
$list = array_merge($list, $errors[$keys[$k]]); |
| 131 |
} |
| 132 |
return $list; |
| 133 |
} |
| 134 |
|
| 135 |
public function render() { |
| 136 |
$errors = $this->parse($this->form->getErrors()); |
| 137 |
if(!empty($errors)) { |
| 138 |
$size = sizeof($errors); |
| 139 |
if($size == 1) |
| 140 |
$format = $this->errorfound; |
| 141 |
else |
| 142 |
$format = str_replace('@errorsize', $size, $this->errorsfound); |
| 143 |
|
| 144 |
echo '<div class="pfbc-error ui-state-error ui-corner-all">', $format, '<ul><li>', implode("</li><li>", $errors), "</li></ul></div>"; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
public function getAjaxErrorResponse() { |
| 149 |
$flatErrors = $this->parse($this->form->getErrors()); |
| 150 |
$elementErrors = $this->form->getErrors(); // Structured errors for ARIA attributes |
| 151 |
return array( |
| 152 |
'errors' => $flatErrors, |
| 153 |
'elementErrors' => $elementErrors |
| 154 |
); |
| 155 |
} |
| 156 |
|
| 157 |
public function renderAjaxErrorResponse() { |
| 158 |
$flatErrors = $this->parse($this->form->getErrors()); |
| 159 |
$elementErrors = $this->form->getErrors(); // Keep structured errors for field-level ARIA |
| 160 |
if(!empty($flatErrors)) { |
| 161 |
header("Content-type: application/json"); |
| 162 |
print _accua_forms_json_encode(array( |
| 163 |
"errors" => $flatErrors, |
| 164 |
"elementErrors" => $elementErrors // Add field-specific errors for ARIA attributes |
| 165 |
)); |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
|