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 微信小程序 用oss下载 aliyun-oss-sdk-6.18.0.min client报错
  • ¥15 ArcGIS批量裁剪
  • ¥15 labview程序设计
  • ¥15 为什么在配置Linux系统的时候执行脚本总是出现E: Failed to fetch http:L/cn.archive.ubuntu.com
  • ¥15 Cloudreve保存用户组存储空间大小时报错
  • ¥15 伪标签为什么不能作为弱监督语义分割的结果?
  • ¥15 编一个判断一个区间范围内的数字的个位数的立方和是否等于其本身的程序在输入第1组数据后卡住了(语言-c语言)
  • ¥15 Mac版Fiddler Everywhere4.0.1提示强制更新
  • ¥15 android 集成sentry上报时报错。
  • ¥15 抖音看过的视频,缓存在哪个文件