2010-07-07 78 views
3

我創建了一個文件一堆錯誤APPPATH/messages/validate.php下了一堆共同消息,如...Can Kohana 3的驗證錯誤是否會繼承?

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文件夾的工作,這就是它被SYSPATH/messages/validate.php下稱爲(見它GitHub)。

我的錯誤消息是否可以從基本文件繼承,還是應該複製每個表單的所有錯誤消息?

回答

2

繼承自動工作,遵循此模式:

  1. 搜索指定的文件中的字段+錯誤的具體信息
  2. 給定文件
  3. 搜索一個在搜索字段+默認消息給定文件中的通用消息
  4. validate文件搜索一個通用消息

因此,如果您超載validate文件並更改默認消息,則繼承將按預期工作。

+0

Hey Shadowhand,謝謝 答案。你的意思是在系統文件夾中更改'validate'嗎?因爲當我將這個'validate'文件複製到'APPPATH/messages'文件夾中時,它不起作用。 – alex 2010-07-22 00:27:51

+1

它應該放在'APPPATH/messages'中。 – shadowhand 2010-07-23 21:48:29

0

這是哈克,但它的作品!

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

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

return array_merge($commonErrors, $errors); 

基本上,它會自動爲您繼承基本規則!

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

模型不應該驗證您的業務邏輯。

4

常見錯誤:APPPATH /消息/ validate.php

​​

特定錯誤:APPPATH /消息/ specific.php

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

Kohana中使用這些序列來查找消息:APPPATH /消息/specific.php,APPPATH/messages/validate.php和SYSPATH/messages/validate.php

print_r(validate->errors('specific'));