is_unique for codeigniter form validation

前端 未结 9 1072
走了就别回头了
走了就别回头了 2020-12-03 07:19

I\'m trying to figure out how I can use the is_unique rule from the Codeigniter form validation library in the following situation.

I\'m trying to submi

9条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-03 07:50

    Here's an easy method that worked for me and uses well documented code (thanks to https://github.com/ivantcholakov for sharing it!). I found it referenced at https://github.com/bcit-ci/CodeIgniter/issues/3109#issuecomment-46346280

    1. Download https://github.com/ivantcholakov/starter-public-edition-3/blob/master/platform/application/libraries/MY_Form_validation.php (MIT licensed) and save it to your application at application\libraries\MY_Form_validation.php
    2. Delete these two lines from __construct():

      $this->CI->load->helper('checkbox'); $this->CI->load->helper('email');

    3. Delete all the functions except __construct() and unique().

    4. At the end of the __construct() method of your controller add this line:

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

    5. As per the documentation of the unique() method update your validation rule to add a "unique" rule like this (e.g. if you already have required and trim rules):

      …|required|unique[tablename.fieldname,tablename.(primaryKey-used-for-updates)]|trim...

提交回复
热议问题