【发布时间】:2017-07-21 12:07:57
【问题描述】:
这里我有一个联系人列表,用户可以在其中添加新联系人以及对其进行编辑。现在在添加时间,我正在使用 codeIgniter 的服务器端验证来减少冗余。另外在这里我有一些用户可以添加他们的联系人。
假设他们是名为“John”和“Sara”的用户。
在这种情况下,每当“john”添加他之前添加的新联系人时,就不能将其添加为重复的联系人号码。
但每当“john”添加之前由“Sara”添加的新联系人时,都可以添加。但它无法添加,因为 CodeIgniter form_validation 检查整个表数据的唯一值。
这是我的控制器代码
$this->form_validation->set_rules('customer_email','Email', 'trim|check_email[customer.customer_email.' .$userId. ']', array('check_email' => 'Email must be unique.'));
$this->session->set_flashdata('check_email','Mobile must be unique');
$this->form_validation->set_rules('customer_mobile', 'Mobile', 'required|is_unique[customer.customer_mobile]');
$this->session->set_flashdata('contact_exists','Mobile must be unique');
if ($this->form_validation->run()) {
$userdata = $this->session->userdata();
$userId = $userdata['id'];
if (!$userId):
redirect(site_url());
endif;
我尝试了很多次但无法解决这个问题。我需要你的善意努力。谢谢
【问题讨论】:
-
删除对
is_unique的检查并为此创建一个callback function。让该函数检查用户名的唯一性。
标签: php codeigniter validation