如何应用“匹配"? Kohana 3.1中的验证规则? [英] How to apply the "matches" validation rule in Kohana 3.1?

查看:39
本文介绍了如何应用“匹配"? Kohana 3.1中的验证规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道如何在Kohana 3.1中应用匹配"验证规则.我在模型中尝试了以下规则,但没有成功:

'password_confirm' => array(
    array('matches', array(':validation', ':field', 'password')),
)

但是它总是失败.我在Valid :: matches()方法的第一行中放置了var_dump($array).我将其粘贴在下面:

/**
 * Checks if a field matches the value of another field.
 *
 * @param   array    array of values
 * @param   string   field name
 * @param   string   field name to match
 * @return  boolean
 */
public static function matches($array, $field, $match)
{
    var_dump($array);exit;
    return ($array[$field] === $array[$match]);
}

它将打印类型为Validation的对象,如果执行var_dump($array[$field]),它将打印null.

非常感谢.

更新:我还从验证消息中得知,规则参数的顺序应与此相反:

'password_confirm' => array(
    array('matches', array(':validation', 'password', ':field')),
)

解决方案

您的语法是正确的,但是我要猜​​测并说您的数据库架构没有'password_confirm'列,因此您尝试添加一个规则不存在的字段.

无论如何,执行密码确认匹配验证的正确位置不在您的模型中,而是在您尝试保存时传递给控制器​​中模型的额外验证.

将其放在您的用户控制器中:

$user = ORM::Factory('user');

// Don't forget security, make sure you sanitize the $_POST data as needed
$user->values($_POST);

// Validate any other settings submitted
$extra_validation = Validation::factory(
    array('password' => Arr::get($_POST, 'password'),
          'password_confirm' => Arr::get($_POST, 'password_confirm'))
    );

$extra_validation->rule('password_confirm', 'matches', array(':validation', 'password_confirm', 'password'));

try 
{
    $user->save($extra_validation);
    // success
}
catch (ORM_Validation_Exception $e)
{               
   $errors = $e->errors('my_error_msgs');
   // failure
}

另外,请参阅 Kohana 3.1 ORM验证文档以获取更多信息

I need to know how to apply the "matches" validation rule in Kohana 3.1. I've tried the following rule in my model with no success:

'password_confirm' => array(
    array('matches', array(':validation', ':field', 'password')),
)

But it always fails. I put a var_dump($array) in the first line of the Valid::matches() method. I paste it below:

/**
 * Checks if a field matches the value of another field.
 *
 * @param   array    array of values
 * @param   string   field name
 * @param   string   field name to match
 * @return  boolean
 */
public static function matches($array, $field, $match)
{
    var_dump($array);exit;
    return ($array[$field] === $array[$match]);
}

It prints an object of type Validation and if I do var_dump($array[$field]) it prints null.

Thanks a lot in advance.

UPDATE: Also I figured out by the validation message that the order of the parameters of the rule should be inverted to this:

'password_confirm' => array(
    array('matches', array(':validation', 'password', ':field')),
)

解决方案

Your syntax is correct, but I'm going to guess and say that your DB schema does not have a 'password_confirm' column so you are trying to add a rule to a field that doesn't exist.

Regardless, the right place to perform password confirm matching validation is not in your model but as extra validation that is passed to your model in your controller when you attempt to save.

Put this in your user controller:

$user = ORM::Factory('user');

// Don't forget security, make sure you sanitize the $_POST data as needed
$user->values($_POST);

// Validate any other settings submitted
$extra_validation = Validation::factory(
    array('password' => Arr::get($_POST, 'password'),
          'password_confirm' => Arr::get($_POST, 'password_confirm'))
    );

$extra_validation->rule('password_confirm', 'matches', array(':validation', 'password_confirm', 'password'));

try 
{
    $user->save($extra_validation);
    // success
}
catch (ORM_Validation_Exception $e)
{               
   $errors = $e->errors('my_error_msgs');
   // failure
}

Also, see the Kohana 3.1 ORM Validation documentation for more information

这篇关于如何应用“匹配"? Kohana 3.1中的验证规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆