代码Igniter自定义字段验证不工作 [英] Code Igniter Custom Field Validation Not Working
问题描述
我在代码Igniter中创建了自定义验证。我有以下代码,但 is_FieldUnique
验证不工作。即使, var_dump()
参数仍然我没有得到它是显示。我下面的代码我做错了什么?我没有得到任何回报与 is_FieldUnique
,但默认验证是工作正常像 required
, min_length
。
I created a custom validation in Code Igniter. I have the following code but the is_FieldUnique
validation is not working. Even if, I var_dump()
the parameters still I am not getting the it was displaying. What am I doing wrong with my code below? I am not getting any return with the is_FieldUnique
but the default validation is working fine like the required
, min_length
.
在 application / libraries / Customfieldvalidation.php
中,我有以下这行代码:
In application/libraries/Customfieldvalidation.php
, I have this line of code below:
class Customfieldvalidation extends CI_Form_validation
{
public function is_FieldUnique($str, $field)
{
var_dump($str);
var_dump($field);
list($table, $field)=explode('.', $field);
$q = $this->CI->db->query("SHOW KEYS FROM $table WHERE Key_name = 'PRIMARY'")->row();
$primary_key = $q->Column_name;
if($this->CI->input->post($primary_key) > 0):
$query = $this->CI->db->limit(1)->get_where($table, array($field => $str,$primary_key.' !='=>$this->CI->input->post($primary_key)));
else:
$query = $this->CI->db->limit(1)->get_where($table, array($field => $str));
endif;
echo $query;
exit();
return $query->num_rows() === 0;
}
}
在我的控制器中, p>
In my controller, I have this code:
class User extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->library('customfieldvalidation');
}
function _setRules()
{
$this->form_validation->set_rules('email_address', 'Email Address', 'required|valid_email|min_length[8]|is_FieldUnique[users.email_address]');
$this->form_validation->set_message('is_FieldUnique', '* must have a unique value');
}
}
可能是自定义字段不是加工?
What could be the reason that the custom field is not working? I know this could be pretty simple for everyone but I'm new to Codeigniter and starting to learn.
推荐答案
您必须正确地加上前缀,才能使用Codeigniter。您的自定义库,如果你是扩展本地库。默认前缀是 MY _
这可以在 application / config / config.php
中更改,使用此配置: $ config ['subclass_prefix'] ='MY _';
。
You must correctly prefix your custom library if you are extending a native library. The default prefix is MY_
this can be changed in application/config/config.php
, with this config: $config['subclass_prefix'] = 'MY_';
.
这样:
class Customfieldvalidation extends CI_Form_validation
be:
class MY_Form_validation extends CI_Form_validation
还要确保文件名反映了这一点并且在适当的位置:
Also ensure that the file name reflects this and is in the appropriate location:
application/libraries/MY_Form_validation.php
您还需要一个构造函数来扩展父类:
You'll also need a constructor to extend the parent class:
function __construct($rules = array())
{
parent::__construct($rules);
}
确保您正在控制器中加载库或 autoload 它。加载库时,不需要包含类前缀:
Ensure that you're loading the library in your controller or autoload it. You don't need to include the class prefix when you load the library:
$this->load->library('form_validation');
我猜你已经在 is_FieldUnique
函数进行调试,但你绝对不希望 exit()
之前的 return
因为这将退出脚本,不允许任何东西返回。我也不清楚我的函数是什么返回(也许我只是昏暗!),所以这样的语句应该有助于:
I'm guessing you've added a few of the lines in your is_FieldUnique
function for debugging, but you definitely don't want exit()
before the return
as this will exit the script and won't allow anything to be returned. It's also not clear to me what the function is returning (maybe I'm just being dim!), so a statement like this should help:
if ($query->num_rows() === 0)
{
return TRUE;
}
else
{
return FALSE;
}
您还可以考虑使用 callback ,这是添加您自己的验证规则的常见方式。
You could also consider using a callback, which is a common way of adding your own validation rules.
这篇关于代码Igniter自定义字段验证不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!