Skip to content

Instantly share code, notes, and snippets.

@bichotll
Last active December 17, 2015 18:50
Show Gist options
  • Save bichotll/5656482 to your computer and use it in GitHub Desktop.
Save bichotll/5656482 to your computer and use it in GitHub Desktop.
How to parse errors with Zend 1, json, jQ...
/* php controller */
//$error = array('error' => $form->getMessages());
//$this->_helper->json( json_encode($error) );
/**/
//post get json objects...
function post_confirmation() {
please_wait_ui();
$.post("/load/confirm",
{
confirm: "done"
}
).done(function(data) {
try {
data = $.parseJSON(data);
console.log(data);
} catch (e) {
console.log(data);
}
if ( data.errors ){
set_errors(data.errors);
}
})
});
//form submit
$(document).ready(function() {
$("#form").submit(function(e) {
//prevent submit form
e.preventDefault();
//......
});
//set errors forms
function set_errors(errors) {
$('.errors').remove();
$.each(errors, function(i, item) {
set_error(i, item);
});
}
function set_error(i, item) {
console.log(i + " - " + item);
var ul_errors = $('<ul class="errors" ></ul>');
$.each(item, function(i2, item2) {
ul_errors.append('<li>' + item2 + '</li>');
});
console.log(ul_errors);
$("#" + i).after(ul_errors);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment