简体   繁体   English

如何在 CodeIgniter 中为 is_unique 表单验证启用查询构建器?

[英]How to enable query builder for is_unique form validation in CodeIgniter?

I have a question about enabling the is_unique() rule for form validation in CodeIgniter.我有一个关于在 CodeIgniter 中为表单验证启用is_unique()规则的问题。

In another explanation ( link ), they don't include the model query builder for standard usage of is_unique()在另一个解释( 链接)中,它们不包括用于is_unique()标准用法的模型查询构建器

I need to use the rule is_unique(table.field) for my id field.我需要将规则is_unique(table.field)用于我的id字段。

What should I do for making this function work on my model file to initiate table.field from my database?我应该怎么做才能使这个函数在我的模型文件上工作以从我的数据库启动table.field Because at documentation , I didn't see an explanation for enabling the is_unique rule.因为在文档中,我没有看到启用is_unique规则的解释。

My current code is still use matching data manually, but I need to know how to use this rules我现在的代码还是手动使用匹配数据,但是我需要知道如何使用这个规则

$this->form_validation->set_rules('siteid', 'Site ID', 'trim|required|max_length[100]|is_unique[site_tower.site_id_tlp]'); $this->form_validation->set_rules('siteid', 'Site ID', 'trim|required|max_length[100]|is_unique[site_tower.site_id_tlp]');

I have just gone through the link you posted, There are 2 ways to use such validation.我刚刚浏览了您发布的链接,有两种方法可以使用此类验证。 If you have set in your configuration files.如果你已经在你的配置文件中设置了。

With that you can use the code as is is_unique[TABLE_NAME.FIELD] and it will work automatically.有了它,您可以按原样使用代码 is_unique[TABLE_NAME.FIELD],它会自动工作。 But at times this logic might not necessarily meet your need and you will need something more complex.但有时这种逻辑可能不一定满足您的需求,您将需要更复杂的东西。

For example lets say you have a members registration that requires you to check if the email already exists, you can run is_unique and it will work perfectly.例如,假设您有一个会员注册,要求您检查电子邮件是否已经存在,您可以运行 is_unique 并且它会完美运行。 Now let's say you want to edit the same member, running is_unique on an edit function will render the user unable to save the data if no data is edited.现在假设您要编辑同一个成员,在编辑函数上运行 is_unique 将使用户无法在未编辑数据的情况下保存数据。 WHY?为什么? because is_unique would determine that the email is already registered although it belongs to the current user that is being edited.因为 is_unique 会确定电子邮件已经注册,尽管它属于正在编辑的当前用户。

How do we fix this?我们如何解决这个问题? We run our own callback in which we specify the logic.我们运行自己的回调,在其中指定逻辑。

You do it by specifying a method within the controller (or a model -- slightly different) but you prefix the method name with callback_ so that it is detected.您可以通过在控制器(或模型 - 略有不同)中指定一个方法来实现,但您在方法名称前加上 callback_ 以便检测到它。

$this->form_validation->set_rules('username', 'Username', 'callback_username_check');

This will then look for a method in your controller called 'username_check'这将在您的控制器中寻找一个名为“username_check”的方法

public function username_check($str)
{
        if ($str == 'test')
        {
                $this->form_validation->set_message('username_check', 'The {field} field can not be the word "test"');
                return FALSE;
        }
        else
        {
                return TRUE;
        }
}

Of course you can use a query within the callback to check against the db rather than check for just a string as it shows in the example.当然,您可以在回调中使用查询来检查数据库,而不仅仅是检查示例中显示的字符串。

more information can be found on Ci3 documentation.更多信息可以在 Ci3 文档中找到。 LINK 关联

Use CTRL + F and search for callback or is_unique使用 CTRL + F 并搜索回调或 is_unique

You might have missed this?你可能错过了这个?

$this->load->library('database');

works instantly after adding database lib.添加数据库库后立即工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM