2016-11-28 6 views
0

CI3の設定ファイルに検証ルールを使用するときにコールバックをどこに配置するのかわかりません。ここに私のform_validation.phpがある:私は上記を実行するとCI3 /設定ファイルへの検証ルールとコールバックの使用

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

$config = array(
    'blog_post' => array(

     array(
     'field' => 'entry_name', 
     'label' => 'entry_name', 
     'rules' => 'min_length[8]|trim|required|max_length[255]' 
     ), 

     array(
     'field' => 'entry_body', 
     'label' => 'entry_body', 
     'rules' => 'trim|required|min_length[12]|callback_html_length_without_html' 
     ), 
    ), 
); 

function html_length_without_html(){ 
    if (2 < 1) 
    { 
     $this->form_validation->set_message('html_length_without_html', 'The field can not be the word'); 
     return FALSE; 
    } else { 
     return TRUE; 
    } 
} 

しかし、私は次のエラーを取得する:

私はコールバック "html_length_without_htmlを()" を配置ん
Unable to access an error message corresponding 
to your field name entry_body.(html_length_without_html) 

+0

ユーザーガイドを読んでいると、その答えがわかります。 https://www.codeigniter.com/userguide3/libraries/form_validation.html#callbacks-your-own-validation-methods – TimBrownlaw

+0

はい、それを読んで、明らかにそれを取得していません。 – user3264461

+0

その機能は、実際の検証を実行しているコントローラで行われます。検証ルールを設定するためにフォーム検証設定を使用しているのと同じコントローラ。 – TimBrownlaw

答えて

1

extendまたはコントローラ内にメソッドを作成することができます。私はヘルパー機能で「伸ばす」ことを好む。

アプリケーション/ヘルパー/ form_validation_helper.phpまたは単にMY_form_helper.phpと拡張は:あなたが$_POSTを使用していると仮定すると、あなたはLANGクラスを使用している場合$ci->form_validation->set_message('html_length_without_html', 'The field can not be the word');

<?php 

if (! defined('BASEPATH')) exit('No direct script access allowed'); 

if (!function_exists('html_length_without_html')) { 

function html_length_without_html() { 
    $ci = & get_instance(); 
    $entry_body = $ci->input->post('entry_body'); 
    /*Do some check here to define if is TRUE or FALSE*/ 
    if ($entry_body < 1) { 
     $ci->form_validation->set_message('html_length_without_html', 'The field can not be the word'); 
     return FALSE; 
    } 
    else { 
     return TRUE; 
    } 
} 

} 

何も間違っている、しかし、あなたは成功したコールバック応答を取得するにはapplication/language/english/form_validation_lang.phpに次の行を保存する必要があります:

$lang['html_length_without_html'] = 'The field can not be the word'; 

使用前にヘルパーをロードすることを忘れないでください。それ:$this->load->helper('form_validation_helper');またはautoload、代わりに。

+0

訂正:これは$ this-> load-> helper( 'form_validation')である必要があります。 _helper.phpの部分はヘルパーとして暗示されています。 – TimBrownlaw

+0

このコメントは削除されました。 – user3264461

+0

POST、PUT(GET、DELETE)を使用しています。もちろん、POSTとPUTは影響を受ける犯人です。これは、Phil SturgeonのAPIサーバーによるものです。 – user3264461

関連する問題