Kohana ORM и проверка, имеющие проблемы

Попытка получить валидацию с ORM, работающей на Kohana 3.2.

На данный момент у меня есть модель:

<?php defined('SYSPATH') or die('No direct access allowed.'); class Model_Brand extends ORM { protected $_has_many = array('models' => array()); protected $_rules = array( 'name' => array( 'not_empty' => NULL, 'min_length' => array(3), 'max_length' => array(20), ), 'sku' => array( 'not_empty' => NULL, 'min_length' => array(3), 'max_length' => array(6), ), ); } 

И heres мой контроллер:

 <?php defined('SYSPATH') or die('No direct script access.'); class Controller_Brand extends Controller_Layout { public function action_view() { $brands = ORM::factory('brand')->find_all(); $this->template->title = __('Brands'); $this->template->content = View::factory('brands/view' ); $this->template->content->set('brands', $brands); } public function action_edit() { if($_POST) { try { $brand = ORM::factory('brand', $this->request->param('id')); $brand->values($_POST); if($brand->check()) { $brand->update(); $brand->save(); //go to brand/views } } catch (ORM_Validation_Exception $e) { //pass errors to brand/edit } } else { $brand = ORM::factory('brand', $this->request->param('id')); $this->template->title = __('Edit Brand'); $this->template->content = View::factory('brands/edit' ); $this->template->content->set('brand', $brand); } } } 

Я еще не добрался до части ошибок. Проблема, с которой я сталкиваюсь, заключается в ее проверке на любом входе и не использовании правил из модели. Также, если кто-нибудь может показать мне, как должно быть разработано такое действие обновления, было бы большой помощью. Благодарю.

Вот как я делаю валидацию модели, и я считаю ее наиболее простой и изящной.

Во-первых, я установил свои правила в методе rules ():

 <?php defined('SYSPATH') or die('No direct access allowed.'); class Model_Brand extends ORM { public function rules() { return array( 'name' => array( array('not_empty'), array('min_length', array(':value', 3)), array('max_length', array(':value', 20)), ) 'sku' => array( array('not_empty'), array('min_length', array(':value', 3)), array('max_length', array(':value', 6)), ) ); ); } 

И вот как я управляю своим действием редактирования:

 public function action_edit() { $brand = ORM::factory('brand', $this->request->param('id')); if (!$brand->loaded()) { throw new Kohana_Exception('Brand not found.'); } $this->template->title = __('Edit Brand'); $this->template->content = View::factory('brands/edit') ->set('brand', $brand) ->bind('errors', $errors); if ($this->request->method() === Request::POST) { try { $brand->values($this->request->post()); $brand->save(); // Success! You probably want to set a session message here. $this->request->redirect($this->request->uri()); } catch(ORM_Validation_Exception $e) { // Fail! $errors = $e->errors('brand'); } } } 

И, на мой взгляд,

 <?php if ($errors) {?> <!-- display errors here --> <?php } ?> <?php echo Form::open()?> <fieldset> <div class="field"> <?php echo Form::label('name', __('Name')), Form::input('name', $brand->name) ?> </div> <?php echo Form::submit('save', 'Save')); ?> </fieldset> <?php echo Form::close()?> 

Как вы можете видеть в представлении, я не выполняю никаких условных проверок, чтобы увидеть, что отображать в поле формы, поскольку это управляется данными в модели, которыми управляет контроллер.

Надеюсь, что это поможет, спросите, если вам нужно уточнить.