2012-01-13 6 views
0

私は完全に検証スクリプトを働いてきた私の問題は、私はここでカスタムエラーメッセージのKohanaのORMの検証

を得ることができないということです登録のための私の関数である。http://pastebin.com/ZF3UVxUr

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

私のメッセージスクリプトは\application\messages\registration.phpにありますか?

ロングコードについては申し訳ありませんが、ちょうどあなたが任意のモデルをヒットしようとする前にポストのデータを検証する必要があるHTMLや他のもの

答えて

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' 
    ) 
); 

また、マイケル・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'); 
} 
+0

私の問題を解決していただきありがとうございますが、何らかの理由で '_external ''が動作しません。それは私に '登録/ユーザを与えます。 username.unique'普通のメッセージのinstedを取得したい – Linas

+0

私は外部のメッセージが別のファイルにある必要があると思います:./application/messages/registration/_external.php – badsyntax

+0

メッセージファイルをチェックすると便利ですコア検証クラスのerrors()メソッド内のパス。 – badsyntax

1

をスキップします。 validation check()を実行していないため、検証ルールは実行されていません。

ように私は何かをするだろう:送信、その後

// ./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' 
    ) 
); 

:Registration.phpはあなたが持っていた「な長さ」スペルの間違いを固定を除いて、ほとんど同じまま

// ./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>'; 
      } 
     } 
    } 
} 

空の「名前」フィールドが返されます。

Array 
(
    [name] => Please enter your username. 
) 
関連する問題