Send error or other type message from server in response to form.save [PHP][Solved]


#1

Please help, I have try many ways but unable to send error text in responce to form.save(). Tryd different methods, events on client side, but unable to send any message.

One of the variants I try:
On server side:

$form = new FormConnector($conn);
function beforeProcessing($action) {
$action->set_response_attribute(‘msg’,‘Server’);
$action->set_response_text(“error”);
$action->invalid();
}
$form->event->attach(“beforeProcessing”,“beforeProcessing”);

On client side:

myForm.attachEvent("onAfterSave", function (id, xml){
  log.msg(xml);
  log.msg(xml.msg);
});
myForm.attachEvent("onAfterUpdate", function (id,action,tid,response){
  log.msg(response);
});

#2

I find out how to pass text, and also make it json. The key of solution is xml.textContent and its not documented.

On server side:

$form = new FormConnector($conn);
function beforeProcessing($action) {
  var $data['error'] = 'text message'
  $action->set_response_text(json_encode($data, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES));
  $action->invalid(); // mark request as invalid so insert/update not called
}
$form->event->attach(“beforeProcessing”,“beforeProcessing”);

On client side:

myForm.attachEvent("onAfterSave", function (id, xml){
  var r = JSON.parse(xml.textContent);
  if (r.error) log.error(r.error);
  //other json processing if needed
}); // log.error from other library, replace it