| 1 |
<?php |
| 2 |
class AccuaForm_Error_Standard extends PFBCError { |
| 3 |
protected $errorfound; |
| 4 |
protected $errorsfound; |
| 5 |
|
| 6 |
public function applyAjaxErrorResponse() { |
| 7 |
$id = $this->form->getId(); |
| 8 |
//$errorfound = _accua_forms_json_encode($this->errorfound); |
| 9 |
//$errorsfound = _accua_forms_json_encode($this->errorsfound); |
| 10 |
echo <<<JS |
| 11 |
var errorSize = response.errors.length; |
| 12 |
var errorHTML = '\\x3Cdiv class="pfbc-error ui-state-error ui-corner-all"\\x3E\\x3Cul\\x3E'; |
| 13 |
for(e = 0; e < errorSize; ++e) { |
| 14 |
errorHTML += '\\x3Cli\\x3E' + response.errors[e] + '\\x3C/li\\x3E'; |
| 15 |
} |
| 16 |
errorHTML += '\\x3C/ul\\x3E\\x3C/div\\x3E'; |
| 17 |
jQuery("#$id").append(errorHTML); |
| 18 |
JS; |
| 19 |
|
| 20 |
} |
| 21 |
|
| 22 |
public function applyAjaxErrorResponseUsingShowErrorMessages() { |
| 23 |
echo <<<JS |
| 24 |
var errorSize = response.errors.length; |
| 25 |
var errorHTML = ''; |
| 26 |
for(e = 0; e < errorSize; ++e) { |
| 27 |
errorHTML += '\\x3Cli\\x3E' + response.errors[e] + '\\x3C/li\\x3E'; |
| 28 |
} |
| 29 |
show_error_messages(errorHTML); |
| 30 |
JS; |
| 31 |
|
| 32 |
} |
| 33 |
|
| 34 |
public function __construct(array $properties = null) { |
| 35 |
$this->errorfound = __('The following error was found:', 'contact-forms'); |
| 36 |
$this->errorsfound = __('The following @errorsize errors were found:', 'contact-forms'); |
| 37 |
$this->configure($properties); |
| 38 |
} |
| 39 |
|
| 40 |
public function setErrorsFound($text = '') { |
| 41 |
$this->errorsfound = $text; |
| 42 |
} |
| 43 |
|
| 44 |
public function setErrorFound($text = '') { |
| 45 |
$this->errorfound = $text; |
| 46 |
} |
| 47 |
|
| 48 |
private function parse($errors) { |
| 49 |
$list = array(); |
| 50 |
if(!empty($errors)) { |
| 51 |
$keys = array_keys($errors); |
| 52 |
$keySize = sizeof($keys); |
| 53 |
for($k = 0; $k < $keySize; ++$k) |
| 54 |
$list = array_merge($list, $errors[$keys[$k]]); |
| 55 |
} |
| 56 |
return $list; |
| 57 |
} |
| 58 |
|
| 59 |
public function render() { |
| 60 |
$errors = $this->parse($this->form->getErrors()); |
| 61 |
if(!empty($errors)) { |
| 62 |
$size = sizeof($errors); |
| 63 |
if($size == 1) |
| 64 |
$format = $this->errorfound; |
| 65 |
else |
| 66 |
$format = str_replace('@errorsize', $size, $this->errorsfound); |
| 67 |
|
| 68 |
echo '<div class="pfbc-error ui-state-error ui-corner-all">', $format, '<ul><li>', implode("</li><li>", $errors), "</li></ul></div>"; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
public function getAjaxErrorResponse() { |
| 73 |
return $this->parse($this->form->getErrors()); |
| 74 |
} |
| 75 |
|
| 76 |
public function renderAjaxErrorResponse() { |
| 77 |
$errors = $this->parse($this->form->getErrors()); |
| 78 |
if(!empty($errors)) { |
| 79 |
header("Content-type: application/json"); |
| 80 |
print _accua_forms_json_encode(array("errors" => $errors)); |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
|