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.

    点赞 评论

相关推荐 更多相似问题