Kohana orm検証
-
28-10-2019 - |
質問
完全に機能している検証スクリプトがあります私の問題は、カスタムエラーメッセージを取得できないということです
登録のための私の機能は次のとおりです。 http://pastebin.com/zf3uvxur
そして、これが私のメッセージ配列です: http://pastebin.com/d9guvm3n
私のメッセージスクリプトは次のとおりです。 \application\messages\registration.php
助言がありますか?
長いコードについて申し訳ありませんが、HTMLなどをスキップするだけです
解決
ユーザーモデルによってスローされる検証例外をキャッチしている場合、メッセージファイルの場所が正しくない可能性があります。次の必要があります:「登録/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');
}
他のヒント
モデルをヒットしようとする前に、投稿データを検証する必要があります。あなたが実行していないため、あなたの検証ルールは実行されていません 検証チェック().
私は次のようなことをします:
// ./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>';
}
}
}
}
登録.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.
)
所属していません StackOverflow