2012-02-03 84 views
3

嘗試使用ORM for Kohana 3.2進行驗證。Kohana 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); 
     } 
    } 
} 

我還沒有到了錯誤的部分呢。我遇到的問題是驗證任何輸入並且不使用模型中的規則。此外,如果任何人都可以告訴我如何設計更新動作應該是一個很大的幫助。謝謝。

回答

4

這是我如何做模型驗證,我覺得它是最直接和最優雅的。

首先,我把我的規則,在規則()方法:

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

正如您在視圖中看到的,我沒有進行任何條件檢查來查看要在表單字段中顯示的內容,因爲這是由數據管理的在由控制器管理的模型中。

希望這會有所幫助,如果您需要進一步澄清,請提出問題。

+0

非常感謝這麼詳細的anwser。正是我在找什麼。 – DanielOR 2012-02-03 18:37:39