Skip to content

Instantly share code, notes, and snippets.

@kemo
Created March 10, 2012 11:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kemo/2011193 to your computer and use it in GitHub Desktop.
Save kemo/2011193 to your computer and use it in GitHub Desktop.
example JSend action
<?php
public function action_feedback()
{
// Only AJAX requests!
if ( ! $this->request->is_ajax())
return $this->redirect();
$feedback = new Model_Testimonial;
$view = new View_Page_Feedback;
$json = JSend::factory(array('html' => $view));
if ($this->request->method() === Request::POST)
{
$csrf = Validation::factory($this->request->post())
->rule('token','not_empty')
->rule('token','Security::check');
try
{
$allowed = array('email','author','text');
$feedback->values($this->request->post(), $allowed)
->create($csrf);
$json->data('feedback', $feedback)
->data('html', new View_Page_FeedbackSuccess);
}
catch (ORM_Validation_Exception $e)
{
$errors = $e->errors('');
$json->data('errors', $errors)
->data('html', $this->view->set('errors', $errors))
->status(JSend::FAIL);
}
catch (Exception $e)
{
$json->message($e);
}
}
$json->render_into($this->response);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment