Php Kohana 3.2验证类存在问题。当出现错误时,它似乎没有发送异常

Php Kohana 3.2验证类存在问题。当出现错误时,它似乎没有发送异常,php,validation,try-catch,kohana-3,Php,Validation,Try Catch,Kohana 3,将POST数据发送到以下位置时: Auth::instance()->register( $_POST ); 然后对数据进行验证。。。如果不是,我假设Kohana中的验证类抛出一个异常。然后尝试catch函数捕获它。我遇到的问题是这种方法: $this->send_confirmation_email($_POST); 即使数据无效,也会调用它。我相信,如果数据无效,它将跳过所有其他内容,并跳到捕捉。。。但似乎我错了,因为我在发送电子邮件的方法中遇到了严重的致命错误,因为它找不到

将POST数据发送到以下位置时:

Auth::instance()->register( $_POST );
然后对数据进行验证。。。如果不是,我假设Kohana中的验证类抛出一个异常。然后尝试catch函数捕获它。我遇到的问题是这种方法:

$this->send_confirmation_email($_POST);
即使数据无效,也会调用它。我相信,如果数据无效,它将跳过所有其他内容,并跳到捕捉。。。但似乎我错了,因为我在发送电子邮件的方法中遇到了严重的致命错误,因为它找不到电子邮件地址

try {
        if( ! $optional_checks ) {
           //throw new ORM_Validation_Exception("Invalid option checks");
        }

        $_POST['email_code'] = Auth::instance()->hash(date('YmdHis', time()));

        Auth::instance()->register( $_POST );

        $this->send_confirmation_email($_POST);

        // sign the user in
        Auth::instance()->login($_POST['username'], $_POST['password']);

        // redirect to the user account
        $this->request->redirect('user/profile');
     } catch (Validation_Exception $e) {
那么,如果数据无效,是否有办法跳过发送电子邮件的方法

可以说我应该使用check()方法。这就是问题的原因:

 Validation::factory($fields)
                    ->rules('username', $this->_rules['username'])
                    ->rule('username', 'username_available', array($this, ':field'))
                    ->rules('email', $this->_rules['email'])
                    ->rule('email', 'email_available', array($this, ':field'))
                    ->rules('password', $this->_rules['password'])
                    ->rules('password_confirm', $this->_rules['password_confirm']);

            if (Kohana::config('useradmin')->activation_code) {
                    Validation::factory($fields)->rule('activation_code', 'check_activation_code', array($this, ':field'));
            }
提前感谢您的帮助

更新:

现在看来,Kohana验证类存在问题

以下是我的Model_用户类中的方法:

public function create_user($fields)
{
    Validation::factory($fields)
        ->rules('username', $this->_rules['username'])
        ->rule('username', 'username_available', array($this, ':field'))
        ->rules('email', $this->_rules['email'])
        ->rule('email', 'email_available', array($this, ':field'))
        ->rules('password', $this->_rules['password'])
        ->rules('password_confirm', $this->_rules['password_confirm']);
        //->labels($_labels);

    if (Kohana::config('useradmin')->activation_code) {
        Validation::factory($fields)->rule('activation_code', 'check_activation_code', array($this, ':field'));
    }

    // Generate a unique ID
    $uuid = CASSANDRA::Util()->uuid1();

    //CASSANDRA::selectColumnFamily('UsersRoles')->insert($username, array('rolename' => 'login'));
    CASSANDRA::selectColumnFamily('Users')->insert($uuid, array(
                            'username'      => $fields['username'],
                            'email'         => $fields['email'],
                            'password'      => Auth::instance()->hash($fields['password']),
                            'logins'        => 0,
                            'last_login'        => 0,
                            'last_failed_login' => 0,
                            'failed_login_count'    => 0,
                            'created'       => date('YmdHis', time()),
                            'modify'        => 0,
                            'role'          => 'login',
                            'email_verified'    => $fields['email_code'],
                        ));
}
验证类之后的代码被执行。因此,即使数据无效,它仍然会向数据库添加新用户

我正在做的测试是使用空输入

规则如下:

protected $_rules = array(
    'username' => array(
        'not_empty' => NULL,
        'min_length' => array(4),
        'max_length' => array(32),
        'regex' => array('/^[-\pL\pN_.]++$/uD'),    
    ),
    'password' => array(
        'not_empty' => NULL,
        'min_length' => array(8),
        'max_length' => array(42),
    ),
    'password_confirm' => array(
        'matches' => array('password'),
    ),
    'email' => array(
        'not_empty' => NULL,
        'min_length' => array(4),
        'max_length' => array(127),
        'validate::email' => NULL,
    ),
);

再次提前感谢您的帮助。

这里有一个叫做“如果其他的话”的东西,您可以使用……;)

您关于try-catch如何工作的想法是正确的:当在try块中抛出异常时,将跳过其中的所有剩余代码,并直接跳转到catch块中


最有可能的是,
register
函数没有像您假设的那样抛出异常,这就是您的代码没有按照您认为应该的方式运行的原因。

有一个叫做if-else的东西,您可以使用…;)

您关于try-catch如何工作的想法是正确的:当在try块中抛出异常时,将跳过其中的所有剩余代码,并直接跳转到catch块中


最有可能的是,
register
函数没有像您假设的那样抛出异常,这可能是您的代码没有执行您认为应该执行的操作的原因。

在这种情况下,Kohana Auth类没有默认的register()方法

我已经在一个特定的案例中实现了这个方法,使用了用于Kohana Useradmin模块的AmfPHP网关

正确的方法是使用model方法添加用户和it角色,如果包含字段的数组无效,您将得到一个提示

您可以从
Auth->instance()->register()获得结果,如:

    if(! Auth::instance()->register( $_POST )) throw new Validation_Exception(...);
尝试或最好的方法是将
Auth->instance()->register($\u POST)
更改为:

    $user->create_user($_POST, array(
        'username',
        'password',
        'email',
    ));
    // Add the login role to the user (add a row to the db)
    $login_role = new Model_Role(array('name' =>'login'));
    $user->add('roles', $login_role);
它将使您的try-catch根据Kohana_验证异常的需要工作

最后,这里是register方法的代码,它可以帮助您:

/**
 * Register a single user
 * Method to register new user by Useradmin Auth module, when you set the
 * fields, be sure they must respect the driver rules
 * 
 * @param array $fields An array witch contains the fields to be populate
 * @returnboolean Operation final status
 * @see Useradmin_Driver_iAuth::register()
 */
public function register($fields) 
{
    if( ! is_object($fields) ) 
    {
        // Load the user
        $user = ORM::factory('user');
    } 
    else 
    {
        // Check for instanced model
        if( $fields instanceof Model_User ) 
        {
            $user = $fields;
        } 
        else 
        { 
            throw new Kohana_Exception('Invalid user fields.');
        }
    }
    try 
    {
        $user->create_user($fields, array(
            'username',
            'password',
            'email',
        ));
        // Add the login role to the user (add a row to the db)
        $login_role = new Model_Role(array('name' =>'login'));
        $user->add('roles', $login_role);
    } 
    catch (ORM_Validation_Exception $e) 
    {
        throw $e;
        return FALSE;
    }
    return TRUE;
}

在本例中,Kohana Auth类没有默认的register()方法

我已经在一个特定的案例中实现了这个方法,使用了用于Kohana Useradmin模块的AmfPHP网关

正确的方法是使用model方法添加用户和it角色,如果包含字段的数组无效,您将得到一个提示

您可以从
Auth->instance()->register()获得结果,如:

    if(! Auth::instance()->register( $_POST )) throw new Validation_Exception(...);
尝试或最好的方法是将
Auth->instance()->register($\u POST)
更改为:

    $user->create_user($_POST, array(
        'username',
        'password',
        'email',
    ));
    // Add the login role to the user (add a row to the db)
    $login_role = new Model_Role(array('name' =>'login'));
    $user->add('roles', $login_role);
它将使您的try-catch根据Kohana_验证异常的需要工作

最后,这里是register方法的代码,它可以帮助您:

/**
 * Register a single user
 * Method to register new user by Useradmin Auth module, when you set the
 * fields, be sure they must respect the driver rules
 * 
 * @param array $fields An array witch contains the fields to be populate
 * @returnboolean Operation final status
 * @see Useradmin_Driver_iAuth::register()
 */
public function register($fields) 
{
    if( ! is_object($fields) ) 
    {
        // Load the user
        $user = ORM::factory('user');
    } 
    else 
    {
        // Check for instanced model
        if( $fields instanceof Model_User ) 
        {
            $user = $fields;
        } 
        else 
        { 
            throw new Kohana_Exception('Invalid user fields.');
        }
    }
    try 
    {
        $user->create_user($fields, array(
            'username',
            'password',
            'email',
        ));
        // Add the login role to the user (add a row to the db)
        $login_role = new Model_Role(array('name' =>'login'));
        $user->add('roles', $login_role);
    } 
    catch (ORM_Validation_Exception $e) 
    {
        throw $e;
        return FALSE;
    }
    return TRUE;
}

我设置规则的方式是错误的,我设置验证自定义规则的方式是错误的。以下是解决问题的代码:

protected $_rules = array(
    'username' => array(
        array('not_empty'),
        array('min_length', array(4)),
        array('max_length', array(32)),
        array('regex', array('/^[-\pL\pN_.]++$/uD')),
    ),
    'password' => array(
        array('not_empty'),
        array('min_length', array(8)),
        array('max_length', array(42)),
    ),
    'password_confirm' => array(
        array('matches', array(':validation', ':field', 'password')),
    ),
    'email' => array(
        array('not_empty'),
        array('min_length', array(4)),
        array('max_length', array(127)),
        array('email'),
    ),
);
验证:

$validation = Validation::factory($fields)
        ->rules('username', $this->_rules['username'])
        ->rule('username', array($this, 'username_available'), array(':validation', ':field'))
        ->rules('email', $this->_rules['email'])
        ->rule('email', array($this, 'email_available'), array(':validation', ':field'))
        ->rules('password', $this->_rules['password'])
        ->rules('password_confirm', $this->_rules['password_confirm'])
        ->errors('register/user');
        //->labels($_labels);

    if (Kohana::config('useradmin')->activation_code) {
        $validation->rule('activation_code', array($this, 'check_activation_code'), array(':validation', ':field'));
    }

    if(!$validation->check())
    {
        throw new Validation_Exception($validation, __('Your registering information is not valid.'));
    }

谢谢大家的支持

我设置规则的方式是错误的,我设置验证自定义规则的方式是错误的。以下是解决问题的代码:

protected $_rules = array(
    'username' => array(
        array('not_empty'),
        array('min_length', array(4)),
        array('max_length', array(32)),
        array('regex', array('/^[-\pL\pN_.]++$/uD')),
    ),
    'password' => array(
        array('not_empty'),
        array('min_length', array(8)),
        array('max_length', array(42)),
    ),
    'password_confirm' => array(
        array('matches', array(':validation', ':field', 'password')),
    ),
    'email' => array(
        array('not_empty'),
        array('min_length', array(4)),
        array('max_length', array(127)),
        array('email'),
    ),
);
验证:

$validation = Validation::factory($fields)
        ->rules('username', $this->_rules['username'])
        ->rule('username', array($this, 'username_available'), array(':validation', ':field'))
        ->rules('email', $this->_rules['email'])
        ->rule('email', array($this, 'email_available'), array(':validation', ':field'))
        ->rules('password', $this->_rules['password'])
        ->rules('password_confirm', $this->_rules['password_confirm'])
        ->errors('register/user');
        //->labels($_labels);

    if (Kohana::config('useradmin')->activation_code) {
        $validation->rule('activation_code', array($this, 'check_activation_code'), array(':validation', ':field'));
    }

    if(!$validation->check())
    {
        throw new Validation_Exception($validation, __('Your registering information is not valid.'));
    }

谢谢大家的支持

哈哈,别担心我会不会担心。但这是我的问题。我需要确保表格中的内容是有效的。我不知道怎么知道它是怎么回事,因为我觉得它会抛出一个错误并跳过其他代码。。。我认为这更像是一个Kohana问题……我添加了一些代码,让您更好地了解正在发生的事情。恐怕我从未使用过Kohana,因此我无法真正帮助您。也许在科哈纳的文件中有关于如何处理这个案件的信息?在我看来,这个例外并没有被发送。。。我在想为什么。。。有一些东西我遗漏了,但我就是找不到。不管怎样,谢谢你的时间和帮助。哈哈,别担心,我想如果还有别的事。但这是我的问题。我需要确保表格中的内容是有效的。我不知道怎么知道它是怎么回事,因为我觉得它会抛出一个错误并跳过其他代码。。。我认为这更像是一个Kohana问题……我添加了一些代码,让您更好地了解正在发生的事情。恐怕我从未使用过Kohana,因此我无法真正帮助您。也许在科哈纳的文件中有关于如何处理这个案件的信息?在我看来,这个例外并没有被发送。。。我在想为什么。。。有一些东西我遗漏了,但我就是找不到。不管怎样,谢谢你的时间和帮助。即使我把:$model\u user->create\u user($\u POST);直接在控制器中,它仍然继续执行。。。它应该在什么时候跳起来抓。这里是模型源代码:我查看我的Cassandra数据库,我可以看到添加的新用户的音调,但没有任何列值。。。因此,验证类似乎没有抛出异常,因为如果它抛出异常,它就不会添加所有那些“无用户”。所以我认为问题在于模型本身和验证。。。你能看看Model_用户并告诉我是什么导致了这个问题吗?提前谢谢。我还尝试在->标签(…)中添加$_标签;在验证中,我得到一个错误,说$\u标签不存在。但在顶部,变量被赋予一个值。。。这没有任何意义,即使我