douhuan7862 2017-07-21 12:07
浏览 59
已采纳

无法使用codeIgniter验证更改值

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):
            redirect(site_url());
        endif;

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

  • 写回答

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
        if($check):
           return 'Contact already exist';
        else:
           // Insert Contact 
        endif;
    }
    

    Let me know if it not works.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么