Trying to get Validation with ORM working for Kohana 3.2.
At the moment i have my Model:
<?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),
),
);
}
And heres my Controller:
<?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);
}
}
}
I haven't even got to the errors part yet. The problem i'm having is its validating on any input and not using the rules from the model. Also if anyone can show me how an update action like this should be designed would be a big help. Thanks.