3

APPPATH/messages/validate.php下のファイルに、次のような一般的なメッセージを含む一連のエラーを作成しました...

return array(
    'not_empty'    => ':field must not be empty.',
    'matches'      => ':field must be the same as :param1',
    'regex'        => ':field does not match the required format',
    'exact_length' => ':field must be exactly :param1 characters long',
    'min_length'   => ':field must be at least :param1 characters long',
    'max_length'   => ':field must be less than :param1 characters long',
    'in_array'     => ':field must be one of the available options',
    'digit'        => ':field must be a digit',
    'email'        => 'You must enter a valid email.',
    'name'         => 'You must enter your name.',
    'enquiry'      => 'You must enter your enquiry.',
    'captcha' => array (
        'Captcha::valid' => 'The characters you entered did not match the image. Please try again.',
        'not_empty' => 'You must enter the characters from the image.'
    ) 
);

これは、のようなエラーが発生した場合にうまく機能し$errors = $post->errors('validate')ます。

これらのエラーを基本エラーとして使用する方法はありますか。さらに必要な別のフォームがある場合は、違いのみを含む別のファイルを使用できます。たとえば、次のようになります。

return array(
    'permissions'    => 'Please agree to the permissions',
);

したがって、明らかに、emailエラーメッセージはvalidate.php(継承された)からpermissions発生しますが、エラーは、のエラー定義を持つ新しいファイルから発生しますpermissions

validate.php継承動作がsystemフォルダーで機能しているように見えるため、ファイルに名前を付けました。これは、その下で呼び出されます( GitHubSYSPATH/messages/validate.phpで参照してください)。

エラーメッセージはベースファイルから継承できますか、それともフォームごとにすべてのエラーメッセージをコピーする必要がありますか?

4

4 に答える 4

4

一般的なエラー:APPPATH / messages / validate.php

return array(
    'email'        => 'You must enter a valid email.',
    'name'         => 'You must enter your name.',
    'enquiry'      => 'You must enter your enquiry.',
    'captcha' => array (
        'Captcha::valid' => 'The characters you entered did not match the image. Please try again.',
        'not_empty' => 'You must enter the characters from the image.'
    )
);

特定のエラー:APPPATH / messages / Specific.php

return array(
    'permissions'    => 'Please agree to the permissions',
);

コハナは次のシーケンスを使用してメッセージを検索します:APPPATH / messages /specific.php、APPPATH / messages / validate.php、SYSPATH / messages / validate.php

print_r(validate->errors('specific'));
于 2010-08-20T18:15:04.470 に答える
3

「ハック」なし:

    $orm->values($form[$this->name])->check();

    $not_model_errors = Validate::factory(array())->rule(NULL, 'permissions_rules'); // doesn't matter what args you send here, just meet the vartype
    // add test error
    $not_model_errors->error(NULL, 'test_error', array());

    $this->template->errors = $orm->validate()->errors('validation') + $not_model_errors->errors('permissions');

モデルはビジネスロジックを検証するべきではありません。

于 2010-07-07T10:40:33.550 に答える
2

継承は自動的に機能します。次のパターンに従ってください。

  1. 指定されたファイルでフィールド+エラー固有のメッセージを検索します
  2. 指定されたファイルでフィールド+デフォルトメッセージを検索します
  3. 指定されたファイルで一般的なメッセージを検索します
  4. ファイル内の一般的なメッセージを検索しvalidateます

したがって、validateファイルをオーバーロードしてデフォルトのメッセージを変更すると、継承は期待どおりに機能します。

于 2010-07-21T14:36:13.077 に答える
0

これはハッキーですが、機能します!

$commonErrors = include APPPATH .  'messages/validate.php';

$errors =  array(
    'permission' => array(
        'not_empty' => 'You must give permission to continue.'
    )
);

return array_merge($commonErrors, $errors);

基本的に、それはあなたのために基本ルールを自動的に継承します!

于 2010-07-07T01:11:20.413 に答える