2017-07-17 13 views
0

このdocumentationに基づいて、2番目のパラメータをルールメソッドに渡す方法は?Codeigniterカスタム検証ルールに余分なパラメータを渡す

これは私のカスタムルール

public function email_exists($email, $exclude_id=NULL) 
{ 
    if ($exclude_id !== NULL) $this->db->where_not_in('id', $exclude_id); 

    $result = $this->db->select('id')->from('users')->where('email', $email)->get(); 

    if ($result->num_rows() > 0) { 
     $this->form_validation->set_message('email_exists', '{field} has been used by other user.'); 
     return FALSE; 
    } else { 
     return TRUE; 
    } 
} 

であり、これは私がemail_existsメソッドの2番目のパラメータを渡すことができますどのように

$rules = [ 
    [ 
     'field' => 'email', 
     'label' => 'Email', 
     'rules' => [ 
      'required', 
      'trim', 
      'valid_email', 
      'xss_clean', 
      ['email_exists', [$this->m_user, 'email_exists']] 
     ] 
    ] 
]; 

$this->form_validation->set_rules($rules); 

コントローラ

からそれを呼び出す方法ですか?これは $exclude_idためのフォームで隠しフィールドを作るよりも、機能していない場合

$this->form_validation->set_rules('uri', 'URI', 'callback_check_uri['.$this->input->post('id').']'); 
// Later: 
function check_uri($field, $id){ 
    // your callback code here 
} 

と直接のことを確認してください。

答えて

0

USTはそれをドキュメントで説明したように正しい方法を(少なくともCI用2.1+)を行いますあなたのコールバックで

$exclude_id = $this->input->post('exclude_id');//or whatever the field name is 

もっとhere

+0

感謝を提供してくれてありがとうはskunkbadするが、私は任意のコントローラからそれを呼び出すことができるようにドキュメントに基づいて、私はモデルでルールを置きます。あなたの提案に従えば、ルールは現在のコントローラでのみ利用可能になります。 – milikpribumi

0

その経由CIは、このためのメカニズムを提供していないようです。私はこれを解決するためにいくつかのアプローチを見つけました。最初の方法は、ファイルシステム(Form_validation.php)をハックし、あなたが可能な第2の方法は、CI_Form_validationコアを拡張し、それにカスタムルールを追加728

if (preg_match('/(.*?)\[(.*)\]/', $rule[1], $rulea)) { 
    $method = $rulea[1]; 
    $extra = $rulea[2]; 
} else { 
    $method = $rule[1]; 
    $extra = NULL; 
} 

$result = is_array($rule) 
    ? $rule[0]->{$method}($postdata, $extra) 
    : $rule($postdata); 

ラインでいくつかのスクリプトを変更することができます。私はこれについての詳細をcodeigniter documentationに見つけました。

<?php 
defined('BASEPATH') OR exit('No direct script access allowed'); 

class MY_Form_validation extends CI_Form_validation 
{ 

    public function __construct() 
    { 
     parent::__construct(); 
    } 

    public function check_conflict_email($str, $exclude_id=NULL) 
    { 
     if ($exclude_id !== NULL) $this->CI->db->where_not_in('id', $exclude_id); 

     $result = $this->CI->db->select('id')->from('users')->where('email', $str)->get(); 

     if ($result->num_rows() > 0) { 
      $this->set_message('check_conflict_email', '{field} has been used by other user.'); 
      return FALSE; 
     } else { 
      return TRUE; 
     } 
    } 

} 

/* End of file MY_Form_validation.php */ 
/* Location: ./application/libraries/MY_Form_validation.php */ 

第3の方法で、これを行うのが最善の方法だと思います。 solution

$rules = [ 
    [ 
     'field' => 'email', 
     'label' => 'Email', 
     'rules' => [ 
      'required', 
      'trim', 
      'valid_email', 
      'xss_clean', 
      [ 
       'email_exists', 
       function($str) use ($second_param){ 
        return $this->m_user->email_exists($str, $second_param); 
       } 
      ] 
     ] 
    ] 
]; 
関連する問題