php - 在使用 CodeIgniter 的表单验证类之前处理输入

标签 php codeigniter validation

既然你不能这样做:$this->form_validation->set_rules($VARIABLE, 'Some text', 'required');,是否可以做类似的事情:

$variable = $this->input->post('some_input');
$variable = some_function_which_manipulates_the_input($variable);

$this->form_validation->set_rules($i_want_the_variable_here, '', '');

在验证检查之前操作输入?添加自定义回调对我来说似乎有点笨拙,因为一个方法可以做几件事(不一定针对 X 验证字段)。

最佳答案

Since you can't do this: $this->form_validation->set_rules($VARIABLE, 'Some text', 'required');

您当然可以这样做,只要$VARIABLE 包含您要验证的字段的name 属性。

看起来您正在传递实际的 $_POST 值作为 set_rules() 的第一个参数 - 它实际上应该是字段名称。请参阅有关设置规则的部分:

http://codeigniter.com/user_guide/libraries/form_validation.html#validationrules

$this->form_validation->set_rules();

The above function takes three parameters as input:

  • The field name - the exact name you've given the form field.
  • A "human" name for this field, which will be inserted into the error message.Names.
  • The validation rules for this form field.

如果您想在验证之前或之后更改输入的实际值,只需添加一个或多个“准备”规则即可。

http://codeigniter.com/user_guide/libraries/form_validation.html#preppingdata

Any native PHP function that accepts one parameter can be used as a rule, like htmlspecialchars, trim, MD5, etc.

Note: You will generally want to use the prepping functions after the validation rules so if there is an error, the original data will be shown in the form.

您也可以使用规则 before,例如,如果您想要 trim() 某些 验证。回调也将起作用,它们与表单验证库的任何函数或任何 vanilla php 函数具有相同的目的 - 通过返回 TRUE/FALSE 验证数据,或更改数据 - 请注意,默认情况下,回调必须属于验证在其中运行的 Controller 。您也可以使用自己的辅助函数,在验证数据时当前脚本可用的任何东西。

关于php - 在使用 CodeIgniter 的表单验证类之前处理输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6377577/

相关文章:

php - 使用 php 将 XML 嵌套到 MySQL 表

php - 无法使用提供的设置连接到数据库服务器。在代码点火器上

php - 在独立的非 Laravel 应用程序中照亮验证器

PHP 有损/无损压缩图像

Joomla Protostar 模板中的 PHP 代码有人可以解释一下这部分吗

php - 在 php codeigniter 中包含另一个页面时,父页面未加载

codeigniter - 使用 Codeigniter 上传到 Amazon S3

HTML5 验证器错误 :Element li is missing one or more of the following attributes: aria-checked, aria-expanded、aria-valuemax、aria-valuem

java - 在 Wicket 表单 validator 中访问任意数量的字段

php - 在 CodeIgniter 中获取 select 语句的空结果