2010-11-22 81 views
0

我深入潜入kohana,我很喜欢它。特别是hmvc的东西等等。 此刻我有问题将我自己的规则添加到验证实例。添加个人验证规则和功能,kohana 3.0.8

验证运行良好,我也认为我自己的函数可以正确调用。但问题是,我自己的验证功能的错误消息不会显示。

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

这里是我的代码删除了一些东西,以缩短它一点:

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

回答

3

您应该添加错误 0致电。请注意,在调用$post->check()之后,验证对象会清除现有错误!你可以通过使用$post->errors()(不带参数)看到它 - 不会有这样的错误信息。

+0

多数民众赞成它,谢谢你很多。有时很容易。 – 2010-11-22 17:23:54