使用表单验证功能"matches"的Codeigniter会在菜单中显示.带子阵列POST [英] Codeigniter using form validation function "matches" with sub-array POST
问题描述
上周刚开始使用CI,就遇到了这个问题.如果将表单数据作为数组传递,在 matches
函数中应放入什么?
Just started with CI last week and got this issue. What to put inside the matches
function if I'm passing the form data as an array?
如果我想传递用户生成的输入(例如多个电话号码或电子邮件),我会使用html表单中的数组来定位单个数组内的所有输入字段.因此,所有内容都放置在这样的数组中:
I use array in the html form to locate all input fields inside single array in case I want to pass user generated input such as multiple phone numbers or emails. So everything is placed in array such as this:
<div>
<label for="password">Password</label>
<input type="password" name="input[password]" id="password" value="<?php echo set_value("input[password]")?>"/>
</div>
<div>
<label for="password">Confirm Password</label>
<input type="password" name="input[conf_password]" id="conf_password" value="<?php echo set_value("input[conf_password]")?>"/>
</div>
注意 * name ="input [password]" *
Notice the *name="input[password]"*
除了我使用功能 matches
$this->form_validation->set_rules("input[password]", "Password", 'required|matches[input[conf_password]]');
$this->form_validation->set_rules("input[conf_password]", "Confirm Password", 'required');
matches[input[conf_password]]
这将不起作用,因为在我检查了Form_Validation.php之后,我发现 matches
将采用我放在 matches
方括号之间的任何字符串,并尝试获取直接来自 $ _ POST
的值.
This will not work because after I checked the Form_Validation.php I found out that matches
will take whatever string I put between the square brackets of matches
and tries to fetch the value from $_POST
directly.
CI码:
/**
* Match one field to another
*
* @access public
* @param string
* @param field
* @return bool
*/
public function matches($str, $field)
{
if ( ! isset($_POST[$field]))
{
return FALSE;
}
$field = $_POST[$field];
return ($str !== $field) ? FALSE : TRUE;
}
所以就不会有 $ _ POST [input [conf_password]]
这样的东西.
So by right there would be no such thing as $_POST[input[conf_password]]
.
我知道我可以使用
- 自定义验证功能
- 直接比较$ _POST ["input"] ["password"] === $ _POST ["input"] ["conf_password"]
我不确定我缺少什么,因为与表单相关的CI中的所有内容都可以很好地与数组配合使用,为什么不能使用此功能?
I'm not sure what I'm missing since everything in CI related to forms is working nicely with arrays, why wouldn't this function?
推荐答案
是的,我有一个类似的问题,CI核心输入无法解决这个问题,我没有通过创建自定义的回调函数来解决我的问题,因为该函数使控制器混乱通常,但是通过扩展 Form_validation
类 MY_Form_validation
Yes, i have a similar problem and there is no way CI core input can solve that, I solved mine not by creating a custom callback function it clutters the controller often, but by extending the Form_validation
class MY_Form_validation
然后我创建了一个名为 matches_array
的函数,然后将其用作 matches_array [inputkeyname --- inputkeyvalue]
then i created a function which i called matches_array
then used as
matches_array[inputkeyname---inputkeyvalue]
你会写成你的
$this>form_validation>set_rules("input[password]","Password",'required|matches_array[input---conf_password]');
这是我记得的功能.
public function matches_array($str, $field)
{
$field = explode('---',$field);
if ( ! isset($theField = $_POST [$field[0] ][ $field[1] ]))
{
return FALSE;
}
return ($str !== $theField) ? FALSE : TRUE;
}
编辑
将其放在您的 app/libraries
上,并将其命名为 MY_Form_validation
, MY _
是您在配置中定义的名称.您在此处输入的所有内容都会自动添加到规则
.
Put it on your app/libraries
and name it MY_Form_validation
, MY_
is what you defined in your config. anything you put in here will be automatically added to the rules
.
class MY_Form_validation extends CI_Form_validation
{
public function __construct($rules = array())
{
parent::__construct($rules);
$this->CI->lang->load('MY_form_validation');
}
//your custom functions
}
这篇关于使用表单验证功能"matches"的Codeigniter会在菜单中显示.带子阵列POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!