Created
March 10, 2012 11:27
-
-
Save kemo/2011193 to your computer and use it in GitHub Desktop.
example JSend action
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 | |
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