【问题标题】:Unable to change value with codeIgniter validation无法使用 codeIgniter 验证更改值
【发布时间】: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


【解决方案1】:

请找到以下解决方案。

首先,您需要从 customer_mobile 中删除 unique 支票。

$this->form_validation->set_rules('customer_mobile', 'Mobile', 'trim|required');

一旦 form_validation 运行为真,然后检查 customer_mobile 与数据库中的用户,如果返回行则 return error 否则 insert Contact

if ($this->form_validation->run() == TRUE) {
    // $check = Check here for customer_mobile with user in database
    if($check):
       return 'Contact already exist';
    else:
       // Insert Contact 
    endif;
}

如果它不起作用,请告诉我。

【讨论】:

  • 使用回调函数不是更好吗...?
  • @JitendraSoftgrid:在回调函数内部你不能传递多个值,在这种情况下,他需要检查contact_number相对于user
  • 是的,但我们像这样使用它Thorpe's answer
  • @JitendraSoftgrid :那不是回调函数,请到这里了解更多关于codeigniter的回调函数。 codeigniter.com/userguide3/libraries/…
  • 完成。谢谢@ShyamShinadiya
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多