Kohana ORM和验证,遇到问题 [英] Kohana ORM and Validation, having problems

查看:89
本文介绍了Kohana ORM和验证,遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试通过适用于Kohana 3.2的ORM进行验证.

Trying to get Validation with ORM working for 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),
        ),

    );

}

这是我的控制器:

<?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.

推荐答案

这就是我进行模型验证的方式,我觉得它最简单,最优雅.

This is how I do model validation, and I feel it's most straightforward and elegant.

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

First, I set my rules in the rules() method:

<?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)),
            )
        );
    );
}

这就是我管理编辑操作的方式:

And this is how I manage my edit action:

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

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

As you can see in the view, I'm not doing any conditional checking to see what to display in the form field, as that is managed by the data in the model, which is managed by the controller.

希望这会有所帮助,请问是否需要进一步说明.

Hope this helps, ask away if you need further clarification.

这篇关于Kohana ORM和验证,遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆