2016-07-14 3 views
0

私はCI3 - form_validation.phpにカスタム検証フォームを持っています。このようなもの:カスタムform_validation.phpでヘルパー機能を使用する - CodeIgniter 3

$config = [ 
'validation_key'=>[ 
    [ 

     'field' => 'name', 
     'label' => 'Name', 
     'rules' => 'trim|required|min_length[2]|max_length[50]|alpha_numeric' 
    ], 
    [ 
     'field' => 'country', 
     'label' => 'name', 
     'rules' => 'in_list[....]' 
    ], 
] 

そして、私はすべてのcontriesの配列を返す関数を持っています。私が望むのは、有効な国名であるかどうかを確認するために、このフィールドヘルパー関数を国フィールドの検証に追加することです。どうやってやるの ? CI3ドキュメントのform_validation.phpにヘルパーを追加する方法はありません。

+0

呼び出し可能 - http://www.codeigniter.com/user_guide/ライブラリ/ form_validation.html#callable-use-anything-as-a-rule – cartalot

答えて

0

あなたがカスタムのコルバックルールを追加し、あなたの中であなたの試合を提供する場合にのみ可能です。このように:

$config = [ 
'validation_key'=>[ 
    [ 

     'field' => 'name', 
     'label' => 'Name', 
     'rules' => 'trim|required|min_length[2]|max_length[50]|alpha_numeric|callback_custom_plugin' 
    ], 
    [ 
     'field' => 'country', 
     'label' => 'name', 
     'rules' => 'in_list[....]' 
    ], 
] 

... 

function custom_plugin($value){ 
    return your_helper_function($value); 
} 
0

configファイル::コントローラ内で

$config = [ 
    'validation_key'=>[ 
       [ 
       'field' => 'name', 
       'label' => 'Name', 
       'rules' => 'trim|required|min_length[2]|max_length[50]|alpha_numeric' 
       ], 
       [ 
       'field' => 'country', 
       'label' => 'name', 
       'rules' => 'required|callback_check_country' 
       ], 
    ] 

function check_country($country) 
    { 
    $countryLists = $this->Your_model->getCountryLists(); 

    //print_r($countryLists); 

    //Something similar 

    if (in_array($country , $countryLists)) { 
     return TRUE; 
    } 
    else 
    { 
     $this->form_validation->set_message('check_country', 'Your Country is not found in list !'); 
     return FALSE; 
    } 
    } 
+0

「あなたに対応するエラーメッセージにアクセスすることができませんrフィールド名国(check_country) "、たとえ私がコールバック関数に" return true "(すべての場合に妥当性検査が偽)を追加したとしても。コールバック関数は正常に動作します(カスタムファイルをチェックインしました) – gdfgdfg

関連する問題