drno94939847 2012-02-03 04:27 采纳率: 0%
浏览 44
已采纳

Kohana ORM和验证,有问题

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.

  • 写回答

1条回答 默认 最新

  • douyue1998 2012-02-03 10:50
    关注

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

    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');
            }
        }
    }
    

    And in my view:

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?