Skip to content

Instantly share code, notes, and snippets.

@lukemorton
Created March 18, 2011 11:37
Show Gist options
  • Save lukemorton/875923 to your computer and use it in GitHub Desktop.
Save lukemorton/875923 to your computer and use it in GitHub Desktop.
Validation without exceptions
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Page extends Controller {
public function action_unpublish()
{
$validation = Model_Page::unpublish($this->request->param('id'), $this->user);
if ($errors = $validation->errors())
{
$this->response->body(
Debug::dump($errors)
);
}
// On success redirect elsewhere
$this->request->redirect(
Route::get('page')->uri()
);
}
}
<?php defined('SYSPATH') or die('No direct script access.');
class Model_Page extends ORM {
public static function unpublish($id, $user)
{
$page = ORM::factory('page', $id)
->values(array(
'status' => 2,
'last_edited_by' => $user->id,
));
$validation = $page->validation();
if ($validation->check())
{
$page->save();
}
return $validation;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment