Php Kohana ORM和验证存在问题

Php Kohana ORM和验证存在问题,php,validation,orm,kohana,Php,Validation,Orm,Kohana,尝试使用为Kohana 3.2工作的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' => arr

尝试使用为Kohana 3.2工作的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),
        ),

    );

}

我就是这样做模型验证的,我觉得这是最简单、最优雅的

首先,我在rules()方法中设置规则:

在我看来:

<?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()?>


哇,谢谢你这么详细的回答。正是我想要的。
<?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()?>