2012-02-03 7 views
3

ORMが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), 
     ), 

    ); 

} 

そしてHERESに私のコントローラ:

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

このような詳細なアンサーに感謝します。まさに私が探していたもの。 – DanielOR