2012-08-08 2 views
1

나는 CodeIgniter의 형태와 유사한 validation.but 동일하지 검증 방법이 datamapper이 datamapper에게 http://datamapper.wanwizard.euDatamapper 검증 - CodeIgniter의

문제가되어 사용하고 있습니다.

예, 모델 관리자 모델 검증 배열 :

public $validation = array(
     'username' => array(
     'rules' => array('unique', 'required', 'trim', 'max_length' => 60, 'min_length' => 3), 
     'label' => 'User' 
    ), 
     'password' => array(
     'rules' => array('required', 'trim', 'encrypt', 'min_length' => 6), 
     'label' => 'Password' 
    ) 
); 

하지만 폼 유효성 검사 배열이 그렇게해야합니다 :

public $form_validation = array(
    array(
     'field' => 'username', 
     'label' => 'User', 
     'rules' => 'unique|required|trim|max_length[60]|min_length[3]' 
    ), 
    array(
     'field' => 'password', 
     'label' => 'Password', 
     'rules' => 'required|trim|encrypt|min_length[6]' 
    ) 
); 

나는 새 관리자를위한 두 개의 수동 검증을하고 싶지 않아 추가 (datamapper 유효성 검사 후 첫 번째 양식 유효성 검사). 나는 단지 하나의 수작업 검증으로이를 수행 할 수있는 방법이 있다고 생각한다.

죄송합니다. 내 영어가 잘못되었습니다. 이해해 주시기 바랍니다. 미리 감사드립니다.

답변

2

CI의 양식 라이브러리없이 Datamapper의 유효성 검사만으로 충분합니다. 당신이 모델을 저장하려고하면

save() 방법은 true 또는 false 저장이 성공하면에 따라 반환합니다. 그렇지 않은 경우 모델의 error property은 실패한 유효성 검증을 위해 생성 된 오류 메시지로 채워야합니다. 메시지는 language files with keys named appropriately에서로드 할 수 있으며 codeigniter의 양식 유효성 검사 라이브러리 form_validaton_lang.php도로드됩니다. 컨트롤러에서

이처럼의 사용을 만들 수 :

Class TheController extends CI_Controller { 
    function save() { 
     // get the model object somehow 
     // ... 
     // update attributes 
     $model->prop0 = $this->input->post('prop0');    
     $model->prop1 = $this->input->post('prop1'); 
     // try to save it 
     if ($model->save()) { 
      // save successful 
      redirect(...); 
     } else { 
      // save failed load form again, with the model 
      $this->load->view('path/to/the/form', array('model' => $model)); 
     } 
    } 
} 

을 뷰는 다음과 같이 일할 수 :

<form method="post" action="..."> 

    <label>prop0</label> 
    <input type="text" name="prop0" value="<?php print $model->prop0?> "> 
    <?php if (!empty($model->error->prop0)):?> 
     <div class="error"><?php print $model->error->prop1; ?></div> 
    <?php endif; ?> 

    <label>prop1</label> 
    <input type="text" name="prop1" value="<?php print $model->prop1?> "> 
    <?php if (!empty($model->error->prop0)):?> 
     <div class="error"><?php print $model->error->prop1; ?></div> 
    <?php endif; ?> 

    <buton type="submit">go</button> 
</form> 

같은 형태로 사용할 수 있습니다 더 이전 모델이 존재하지 않을 때 데이터베이스, 그냥 모델의 빈 인스턴스를 만들고 양식에 전달하십시오.