3
嘗試使用ORM for Kohana 3.2進行驗證。Kohana ORM和驗證,有問題
目前,我有我的模型:
<?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),
),
);
}
而且我的繼承人控制器:
<?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);
}
}
}
我還沒有到了錯誤的部分呢。我遇到的問題是驗證任何輸入並且不使用模型中的規則。此外,如果任何人都可以告訴我如何設計更新動作應該是一個很大的幫助。謝謝。
非常感謝這麼詳細的anwser。正是我在找什麼。 – DanielOR 2012-02-03 18:37:39