0

検証スクリプトが完全に機能しています。問題は、カスタムエラーメッセージを取得できないことです。

これが私の登録機能です:http://pastebin.com/ZF3UVxUr

そして、これが私のメッセージ配列です:http: //pastebin.com/d9GUvM3N

私のメッセージスクリプトは次のとおりです:\application\messages\registration.php何か提案はありますか?

長いコードについては申し訳ありませんが、htmlやその他のものをスキップしてください

4

2 に答える 2

2

User モデルによってスローされた検証例外をキャッチしている場合は、メッセージ ファイルの場所が正しくない可能性があります。'registration/user.php' である必要があります。

// ./application/messages/registration/user.php
return array(
    'name' => array(
        'not_empty' => 'Please enter your username.',
    ),
    'password' => array(
        'matches' => 'Passwords doesn\'t match',
        'not_empty' => 'Please enter your password'
    ),
    'email' => array(
        'email' => 'Your email isn\'t valid',
        'not_empty' => 'Please enter your email'
    ),
    'about-me' => array(
        'max_length' => 'You cann\'ot exceed 300 characters limit'
    ),
    '_external' => array(
        'username' => 'This username already exist'
    )
);

また、Michael P の回答に反して、すべての検証ロジックをモデルに格納する必要があります。新しいユーザーを登録するためのコントローラー コードは、次のように単純にする必要があります。

try
{
  $user->register($this->request->post());

  Auth::instance()->login($this->request->post('username'), $this->request->post('password'));
}
catch(ORM_Validation_Exception $e) 
{
  $errors = $e->errors('registration');
}
于 2012-01-14T11:55:37.820 に答える
1

モデルをヒットする前に、投稿データを検証する必要があります。検証 check()を実行していないため、検証ルールは実行されていません。

私は次のようなことをします:

// ./application/classes/controller/user
class Controller_User extends Controller
{

    public function action_register()
    {

        if (isset($_POST) AND Valid::not_empty($_POST)) {
            $post = Validation::factory($_POST)
                ->rule('name', 'not_empty');

            if ($post->check()) {
                try {
                    echo 'Success';
                    /**
                    * Post is successfully validated, do ORM
                    * stuff here
                    */
                } catch (ORM_Validation_Exception $e) {
                    /**
                    * Do ORM validation exception stuff here
                    */
                }
            } else {
                /**
                * $post->check() failed, show the errors
                */
                $errors = $post->errors('registration');

                print '<pre>';
                print_r($errors);
                print '</pre>';
            }
        }
    }
}

Registration.php は、「長さ」のスペルミスを修正することを除いて、ほとんど同じままです。

// ./application/messages/registration.php
return array(
    'name' => array(
        'not_empty' => 'Please enter your username.',
    ),
    'password' => array(
        'matches' => 'Passwords doesn\'t match',
        'not_empty' => 'Please enter your password'
    ),
    'email' => array(
        'email' => 'Your email isn\'t valid',
        'not_empty' => 'Please enter your email'
    ),
    'about-me' => array(
        'max_length' => 'You cann\'ot exceed 300 characters limit'
    ),
    '_external' => array(
        'username' => 'This username already exist'
    )
);

次に、空の「名前」フィールドを送信すると、次が返されます。

Array
(
    [name] => Please enter your username.
)
于 2012-01-13T22:43:21.553 に答える