添加个人验证规则和功能,kohana 3.0.8

时间:2010-11-22 14:36:04

标签: validation kohana

我正深入kohana,我非常喜欢它。特别是hmvc的东西等等。 目前我在将自己的规则添加到验证实例时遇到问题。

验证运行良好,我也认为我自己的函数被正确调用。但问题是我的验证功能的错误信息没有显示。

也许有人可以查看代码,看看我做错了什么。谢谢!

这是我的代码我删除了一些东西来缩短它:

    class Controller_Bookmarks extends Controller_DefaultTemplate
    {

    public function action_create_bookmark()
    {
   $posts                            = new Model_Post();

   if($_POST){
   $post = new Validate($_POST,$_FILES);

   //attaching rules
   $post  ->rule('bookmark_title', 'not_empty')
     ->rule('bookmark_image', 'Model_Post::email_change');

   //add error for custom functionm
   $post->error('bookmark_image', 'email_change');


   if ($post->check())
   {
    echo 'yeah';
   }else{
    print_r($post->errors('validate'));
   }
  }else{

  }

  $this->template->content          = View::factory('pages/create_bookmark');
  }
    }

我的模特:

class Model_Post extends Kohana_Model
 {

 public function email_change($str)
 {


  return false;

 }


 }

我的错误消息定义消息/ validate.php(仅用于测试):

     <?php defined('SYSPATH') or die('No direct script access.');<br />
    return array(
   'alpha'         => ':field must contain only letters',
   'alpha_dash'    => ':field must contain only letters and dashes',
   'alpha_numeric' => ':field must contain only letters and numbers',
   'color'         => ':field must be a color',
   'credit_card'   => ':field must be a credit card number',
   'date'          => ':field must be a date',
   'decimal'       => ':field must be a decimal with :param1 places',
   'digit'         => ':field must be a digit',
   'email'         => ':field must be a email address',
   'email_domain'  => ':field must contain a valid email domain',
   'exact_length'  => ':field must be exactly :param1 characters long',
   'in_array'      => ':field must be one of the available options',
   'ip'            => ':field must be an ip address',
   'matches'       => ':field must be the same as :param1',
   'min_length'    => ':field must be at least :param1 characters long',
   'max_length'    => ':field must be less than :param1 characters long',
   'phone'         => ':field must be a phone number',
   'not_empty'     => ':field rrrrrrrrrrrrrrrrrrrrr must not be empty',
   'range'         => ':field must be within the range of :param1 to :param2',
   'regex'         => ':field does not match the required format',
   'url'           => ':field must be a url',
   'email_change'  => ':field gdffddfgdfhgdfhdfhhdfhdfhdfhd',
   );

1 个答案:

答案 0 :(得分:3)

您应该在 ->check()内部添加错误。请注意,在您调用$post->check()后,Validate对象会清除现有错误!你可以使用$post->errors()(没有参数)看到它 - 没有这样的错误信息。