ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

same rule for is_unique when editing and adding entries

February 22, 2012 5:14am

Subscribe [3]
  • #1 / Feb 22, 2012 5:14am

    keithics

    33 posts

    Hi guys,

    I hope this will help you.

    Problem:
    When adding a new entry with is_unique will work just fine but what about when you want to update the entry using the same form validation rule? is_unique will not work because it will detect that you are adding a duplicate entry. Which sucks, and you will be forced to create a new rule just for that.


    Prerequisites:
    1. when editing, you must pass the primary key as post data (which we mostly do)
    Example where user_id is the primary key:

    <input type="hidden" value="1" name="user_id" />

    Benefits:
    1. Same form validation rule for editing and adding

    Code:

    <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class My_Form_validation extends CI_Form_validation {
     
     public function is_unique($str, $field)
     {
      list($table, $field)=explode('.', $field);
      $q = $this->CI->db->query("SHOW KEYS FROM $table WHERE Key_name = 'PRIMARY'")->row();
      $primary_key = $q->Column_name;
      
      if($this->CI->input->post($primary_key) > 0):
       $query = $this->CI->db->limit(1)->get_where($table, array($field => $str,$primary_key.' !='=>$this->CI->input->post($primary_key)));
      else:
       $query = $this->CI->db->limit(1)->get_where($table, array($field => $str));
      endif;
      
      return $query->num_rows() === 0;
        }
    }
    
    
    /* End of file My_Form_validation.php */
    /* Location: ./application/libraries/My_Form_validation.php */
  • #2 / Feb 22, 2012 12:35pm

    Tominator

    210 posts

    I’m solving that problem by copying rules (showed below) in my form_validation config:

    // after setting post/create rules
    
    $config['post/update'] = $config['post/create'];
    $config['post/create'][0]['rules'] .= '|is_unique[post.title]';
  • #3 / Feb 22, 2012 8:11pm

    keithics

    33 posts

    that would probably solve it, if you have a lot of tables with is_unique rules.. you will be forced to copy and pasting each of it.

  • #4 / May 04, 2012 10:20am

    gte451f

    4 posts

    I like this approach!

    I don’t have to maintain 2 rules, one for edit and one for insert.  The passive check should be pretty good but could lead to problems when the table contains composite keys.

    Thanks for the code suggestion!

  • #5 / Sep 30, 2012 7:10pm

    rei

    67 posts

    Hi, How can we set rules using this? I mean how can we pass the primary key to the rules? Can u please give me a sample usage on how to use this rule.Thanks 😊

  • #6 / Sep 30, 2012 8:42pm

    keithics

    33 posts

    the function only overrides that current is_unique method. that means you can use it the same way.

    is_unique[users.username]

    the primary key is automatically detected.

    But as gte451f said, if you have Composite Primary Keys in your table, it wont work.

    http://stackoverflow.com/questions/217945/can-i-have-multiple-primary-keys-in-a-single-table

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases