将其他POST变量传递到Kohana 3中的验证规则的最佳方法是什么?

时间:2021-08-05 20:26:34

I'm trying to validate some POST data. One of the validations I need to do is a registration code, which is based off another POST variable - an IMEI number.

我正在尝试验证一些POST数据。我需要做的一个验证是注册代码,它基于另一个POST变量 - 一个IMEI号。

In my POST data I have 2 fields, register_imei and register_code. My code currently looks like this:

在我的POST数据中,我有2个字段,register_imei和register_code。我的代码目前看起来像这样:

$post = Validate::factory($_POST);

$post->rule('register_imei', 'not_empty')
     ->rule('register_imei', 'exact_length', array(15))
     ->rule('register_imei', 'some_class::luhn_check');

$post->rule('register_code', 'not_empty')
     ->rule('register_code', 'some_class::valid_registration_code', array($_POST['register_imei']));

However, I'm not sure whether passing in the variable from the raw POST array field is ok, because it could be empty or not set. Does the fact that I've already added validation rules for register_imei above make it safe?

但是,我不确定从原始POST数组字段传入变量是否正常,因为它可能为空或未设置。我已经为上面的register_imei添加了验证规则这一事实是否安全?

1 个解决方案

#1


0  

Does the fact that I've already added validation rules for register_imei above make it safe?

我已经为上面的register_imei添加了验证规则这一事实是否安全?

No validation is taken place until you call the check() method.

在调用check()方法之前,不会进行任何验证。

To solve your problem, use:

要解决您的问题,请使用:

Arr::get($_POST, 'register_imei', NULL);

which returns the 3rd argument as a default if the key is not set in the array.

如果未在数组中设置键,则返回第3个参数作为默认值。

#1


0  

Does the fact that I've already added validation rules for register_imei above make it safe?

我已经为上面的register_imei添加了验证规则这一事实是否安全?

No validation is taken place until you call the check() method.

在调用check()方法之前,不会进行任何验证。

To solve your problem, use:

要解决您的问题,请使用:

Arr::get($_POST, 'register_imei', NULL);

which returns the 3rd argument as a default if the key is not set in the array.

如果未在数组中设置键,则返回第3个参数作为默认值。