| 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 = json_encode($this->errorfound, JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_QUOT|JSON_HEX_AMP); |
| 9 |
//$errorsfound = json_encode($this->errorsfound, JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_QUOT|JSON_HEX_AMP); |
| 10 |
echo <<<JS |
| 11 |
var errorSize = response.errors.length; |
| 12 |
var errorHTML = '<div class="pfbc-error ui-state-error ui-corner-all"><ul>'; |
| 13 |
for(e = 0; e < errorSize; ++e) |
| 14 |
errorHTML += '<li>' + response.errors[e] + '</li>'; |
| 15 |
errorHTML += '</ul></div>'; |
| 16 |
jQuery("#$id").append(errorHTML); |
| 17 |
JS; |
| 18 |
|
| 19 |
} |
| 20 |
|
| 21 |
public function __construct(array $properties = null) { |
| 22 |
$this->errorfound = __('The following error was found:', 'accua-form-api'); |
| 23 |
$this->errorsfound = __('The following @errorsize errors were found:', 'accua-form-api'); |
| 24 |
$this->configure($properties); |
| 25 |
} |
| 26 |
|
| 27 |
public function setErrorsFound($text = '') { |
| 28 |
$this->errorsfound = $text; |
| 29 |
} |
| 30 |
|
| 31 |
public function setErrorFound($text = '') { |
| 32 |
$this->errorfound = $text; |
| 33 |
} |
| 34 |
|
| 35 |
private function parse($errors) { |
| 36 |
$list = array(); |
| 37 |
if(!empty($errors)) { |
| 38 |
$keys = array_keys($errors); |
| 39 |
$keySize = sizeof($keys); |
| 40 |
for($k = 0; $k < $keySize; ++$k) |
| 41 |
$list = array_merge($list, $errors[$keys[$k]]); |
| 42 |
} |
| 43 |
return $list; |
| 44 |
} |
| 45 |
|
| 46 |
public function render() { |
| 47 |
$errors = $this->parse($this->form->getErrors()); |
| 48 |
if(!empty($errors)) { |
| 49 |
$size = sizeof($errors); |
| 50 |
if($size == 1) |
| 51 |
$format = $this->errorfound; |
| 52 |
else |
| 53 |
$format = str_replace('@errorsize', $size, $this->errorsfound); |
| 54 |
|
| 55 |
echo '<div class="pfbc-error ui-state-error ui-corner-all">', $format, '<ul><li>', implode("</li><li>", $errors), "</li></ul></div>"; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
public function renderAjaxErrorResponse($return = false) { |
| 60 |
$errors = $this->parse($this->form->getErrors()); |
| 61 |
if ($return) { |
| 62 |
return $errors; |
| 63 |
} |
| 64 |
if(!empty($errors)) { |
| 65 |
header("Content-type: application/json"); |
| 66 |
echo json_encode(array("errors" => $errors), JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_QUOT|JSON_HEX_AMP); |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|