6

登録を @mywork.com のメールに限定したい My_Form_validation で以下を作成しました。

public function email_check($email)
    {
        $findme='mywork.com';
        $pos = strpos($email,$findme);
        if ($pos===FALSE)
        {
            $this->CI->form_validation->set_message('email_check', "The %s field does not have our email.");
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }

私は次のように使用します。ユーザー名とパスワードにCIルールを使用していますが、それは機能します。メールの場合は、任意のメールアドレスを受け入れます。どんな助けにも感謝します。

function register_form($container)
    {
....
....

/ Set Rules
$config = array(
...//for username
// for email            
    array(
  'field'=>'email',
  'label'=>$this->CI->lang->line('userlib_email'),
  'rules'=>"trim|required|max_length[254]|valid_email|callback_email_check|callback_spare_email"
   ),
...// for password
 );

$this->CI->form_validation->set_rules($config);
4

3 に答える 3

18

コントローラーでコールバックを直接作成する際の問題は、http://localhost/yourapp/yourcontroller/yourcallback望ましくない呼び出しによって URL でアクセスできるようになったことです。検証ルールを構成ファイルに入れる、よりモジュール化されたアプローチがあります。私はお勧め:

あなたのコントローラー:

<?php
class Your_Controller extends CI_Controller{
    function submit_signup(){
        $this->load->library('form_validation');
        if(!$this->form_validation->run('submit_signup')){
            //error
        }
        else{
            $p = $this->input->post();
            //insert $p into database....
        }
    }
}

application/config/form_validation.php:

<?php
$config = array
(   
    //this array key matches what you passed into run()
    'submit_signup' => array
    (
        array(
            'field' => 'email',
            'label' => 'Email',
            'rules' => 'required|max_length[255]|valid_email|belongstowork'
        )
        /*
        ,
        array(
            ...
        )
        */

    )
    //you would add more run() routines here, for separate form submissions.
);

application/libraries/MY_Form_validation.php:

<?php
class MY_Form_validation extends CI_Form_validation{    
     function __construct($config = array()){
          parent::__construct($config);
     }
     function belongstowork($email){
         $endsWith = "@mywork.com";
         //see: http://stackoverflow.com/a/619725/568884
         return substr_compare($endsWith, $email, -strlen($email), strlen($email)) === 0;
     }
}

application/language/english/form_validation_lang.php:

追加:$lang['belongstowork'] = "Sorry, the email must belong to work.";

于 2012-10-14T03:46:36.443 に答える
0

Codeigniter コールバック関数でこのような検証が必要ですか?

$this->form_validation->set_rules('email', 'email', 'trim|required|max_length[254]|valid_email|xss_clean|callback_spare_email[' . $this->input->post('email') . ']');

if ($this->form_validation->run() == FALSE)
{
    // failed
    echo 'FAIL';
}
else
{ 
    // success
    echo 'GOOD';
}

function spare_email($str)
{

    // if first_item and second_item are equal
    if(stristr($str, '@mywork.com') !== FALSE)
    {
    // success
    return $str;
    }
    else
    {
    // set error message
    $this->form_validation->set_message('spare_email', 'No match');

    // return fail
    return FALSE;
    }
}  
于 2012-10-14T03:22:35.380 に答える
0

ヨルダンの答えへの修正、編集する必要がある言語ファイルは次の場所にあります

システム/言語/英語/form_validation_lang.php

application/.../form_validation_lang.php ではありません。アプリケーション パスの下に同じ名前の新しいファイルを作成すると、システム パス内の元のファイルが上書きされます。したがって、元のフィルターをすべて使用できなくなります。

于 2014-06-02T20:58:28.150 に答える