【问题标题】:adding individual validation rule and function, kohana 3.0.8添加个人验证规则和功能,kohana 3.0.8
【发布时间】:2010-11-22 14:36:03
【问题描述】:

我正在深入了解 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;

 }


 }

我的错误信息定义messages/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',
   );

【问题讨论】:

    标签: validation kohana


    【解决方案1】:

    您应该在内部 -&gt;check() 调用中添加错误。请注意,在您调用$post-&gt;check() 之后,验证对象会清除现有错误!你可以使用$post-&gt;errors()(不带参数)看到它——不会有这样的错误信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多