Kohana ORM和Validation,存在问题


Kohana ORM and Validation, having problems

尝试在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),
        ),
    );
}

这是我的控制器:

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

正如您在视图中看到的,我没有进行任何条件检查来查看在表单字段中显示什么,因为这是由控制器管理的模型中的数据管理的。

希望这能有所帮助,如果你需要进一步澄清,请询问。