Created
January 9, 2015 11:30
-
-
Save delboy1978uk/5ef4e1befa767632063c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Website\Controller\Action; | |
use Website\Form\Contact as ContactForm; | |
use Object\Enquiry; | |
class DefaultController extends Action | |
{ | |
public function defaultAction() | |
{ | |
$this->enableLayout(); | |
$this->setLayout('default'); | |
$form = new ContactForm(); | |
if($this->getRequest()->isPost()) | |
{ | |
$this->disableLayout(); | |
$this->_helper->viewRenderer->setNoRender(); | |
$data = $this->getRequest()->getParams(); | |
header('Content-Type: application/json'); | |
if($form->isValid($data)) | |
{ | |
try | |
{ | |
$enquiry = Enquiry::create($form->getValues()); | |
$enquiry->save(); | |
die(json_encode(array('msg' => 'OK'))); | |
} | |
catch(Exception $e) | |
{ | |
$form->populate($data); | |
die(json_encode(array('msg' => 'FAIL', 'form' => $form->render(),'error' => $e->getMessage()))); | |
} | |
} | |
else | |
{ | |
$form->populate($data); | |
die(json_encode(array('msg' => 'FAIL', 'form' => $form->render(),'error' => 'Failed Validation'))); | |
} | |
} | |
$this->view->form = $form; | |
} | |
public function contactAction() | |
{ | |
$this->enableLayout(); | |
$this->setLayout('default'); | |
$this->view->form = $form = new ContactForm(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment