2017-07-21 12:07
浏览 54


Here i have a list of contacts where user can add new contacts as well as edit them. Now at the add time i'm using server-side validation by codeIgniter for reduce redundancies. Also here i have a number of users that can add their contacts.

Suppose their are users named "John" and "Sara".

In this case, whenever "john" add new contact that are prevoiusly added by him, then it can't be add as duplicate contact number.

But whenever "john" add new contact that are previously added by "Sara", them it can be add . but its unable to add because CodeIgniter form_validation check unique value with entire table data.

Here is my Controller Code

 $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):

I trying to many time but unable to fix this issue. I need your kind efforts. Thanks

图片转代码服务由CSDN问答提供 功能建议

这里我有一个联系人列表,用户可以在其中添加新联系人以及编辑它们。 现在在添加时我正在使用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'=>'电子邮件必须是唯一的。')); 
 $ this-> session-> set_flashdata('check_email','移动必须是唯一'); 
 $ this-> form_validation  - > set_rules('customer_mobile','Mobile','required | is_unique [customer.customer_mobile]'); 
 $ this-> session-> set_flashdata('contact_exists','Mobile必须是唯一');  
 if($ this-> form_validation-> run()){
 $ userdata = $ this-> session-> userdata(); 
 $ userId = $ userdata ['id  ']; 
 if(!$ userId):

我尝试了很多次但无法 解决这个问题。 我需要你的努力。 谢谢

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dsfs64664 2017-07-21 12:21

    Please find below solution.

    First you need to remove unique check from customer_mobile.

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

    Once form_validation runs true then check customer_mobile with user in database and if it return row then return error else insert Contact.

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

    Let me know if it not works.

    点赞 评论

相关推荐 更多相似问题