Skip to content

Instantly share code, notes, and snippets.

@delboy1978uk
Created January 9, 2015 11:30
Show Gist options
  • Save delboy1978uk/5ef4e1befa767632063c to your computer and use it in GitHub Desktop.
Save delboy1978uk/5ef4e1befa767632063c to your computer and use it in GitHub Desktop.
<?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