在 Kohana 3 中将附加 POST 变量传递到验证规则中的最佳方法是什么?
我正在尝试验证一些 POST 数据。我需要做的验证之一是注册码,它基于另一个 POST 变量 - IMEI 号码。
在我的 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']));
但是,我不确定从原始 POST 数组字段传递变量是否可以,因为它可能为空或未设置。我已经为上面的 register_imei
添加了验证规则,这是否安全?
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.
In my POST data I have 2 fields, register_imei
and register_code
. My code currently looks like this:
$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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在调用
check()
方法之前不会进行任何验证。要解决您的问题,请使用:
如果未在数组中设置键,它将返回第三个参数作为默认值。
No validation is taken place until you call the
check()
method.To solve your problem, use:
which returns the 3rd argument as a default if the key is not set in the array.