Hello,
I am making an Ajax call to a controller/action on form submission. The controller action does basic validation and saves the content to the database.
public function indexAction()
{
$form = new ContactsForm();
$this->view->disable();
$message = "Oops! Something went wrong. Please try again later.";
$response = new \Phalcon\Http\Response();
$response->setStatusCode(400, "Bad Request");
if ($this->request->isPost() && $this->request->isAjax()) {
$params = array(
'name' => 'emailFooter',
'email' => $this->request->getPost('email'),
'businessSize' => 'N/A'
);
if ($form->isValid($params) != false) {
$user = new Contacts();
$user->assign($params);
if ($user->save()) { // Here is where a network call is made in model->afterSave()
$message = "Thanks - We'll let you know when the product is available!";
$response->setStatusCode(200);
}
} else {
$message = $form->getMessages()[0]->getMessage();
}
}
$response->setJsonContent($message);
$response->send();
exit;
}
The issues comes when I call $user->save() because that model has an afterSave() method that will send an email to the user via a API request to AWS SES. This means that instead of me returning the explicit message/status I want the javascsript always gets the response from email API network call instead.
/**
* Send a confirmation e-mail to the user
*/
public function afterSave()
{
// Get first name if possible
$nameParts = explode(" ", $this->name);
$this->getDI()
->getMail()
->send(array(
$this->email => $this->name
), "Welcome", 'general-submission', array("name" => $nameParts[0]));
}
Is there anyway to have Phalcon/Ajax ignore the HTTP response of the AWS mail API call so I can explicitely return what I want?