Welcome Guest, Not a member yet? Register   Sign In
is_unique in Form Validation on UPDATE
#1

We have a table that has a section called "slug".
Obviously slug needs to be unique and therefore when we insert we use "is_unique[article.slug]" which works perfectly fine.

However the problem is that when we want to edit that slug we need it to again check if is_unique BUT not compare it with itself.

Is there a right way around this, because at the moment when we submit the update and slug is not changed it considers it "used" and therefore passes an error.

Thanks.
Reply
#2

I do a similar thing in my app with username/email address checking.

I simply created a custom validation function and model function which checks if it's unique, but has a parameter "exclude_ids", which I use to pass the user's id through to exclude it from the results returned.

I suggest doing the same, as the built-in "is_unique" function won't give you much control/flexibility like this.
Reply
#3

for me, if i have to check differents values on UPDATE or INSERT, I create 2 validation rules.

// user_validation_insert => 'email' => 'valid_email|is_unique[user.email]'

// user_validation_update => 'email' => 'valid_email'


something like that
Reply
#4

(06-06-2016, 04:29 AM)edoramedia Wrote: We have a table that has a section called "slug".
Obviously slug needs to be unique and therefore when we insert we use "is_unique[article.slug]" which works perfectly fine.

However the problem is that when we want to edit that slug we need it to again check if is_unique BUT not compare it with itself.

Is there a right way around this, because at the moment when we submit the update and slug is not changed it considers it "used" and therefore passes an error.

Thanks.

Why edit the slug? My opinion, just update the title without change the slug. Updating the slug cause "404 not found" when bot re-crawling your web page or some visitor had bookmark your page URL.
Keep calm.
Reply
#5

(06-06-2016, 05:27 AM)keulu Wrote: for me, if i have to check differents values on UPDATE or INSERT, I create 2 validation rules.

// user_validation_insert => 'email' => 'valid_email|is_unique[user.email]'

// user_validation_update => 'email' => 'valid_email'


something like that

But what if you changed your email when editing? It still has to be unique, so the check has to be run, otherwise you could change it to an email which already exists.
Reply
#6

(This post was last modified: 06-06-2016, 07:17 AM by PaulD.)

If you want to do this, just run a search for the slug and count results in your own validation routine. Then you can exclude the current record easily. However you should not really be changing the page URL without the user agreeing to it, or even being in control of it. Why not add the slug to the form and put an instruction/warning that changing it will change the page url. Also, you can make sure the column is defined as unique and catch the error when updating.
Reply
#7

Code:
// The following rule has been "borrowed" from
    // Bonfire application starter, http://cibonfire.com/
    /**
     * Checks that a value is unique in the database.
     *
     * i.e. '…|required|unique[users.name,users.id]|trim…'
     *
     * <code>
     * "unique[tablename.fieldname,tablename.(primaryKey-used-for-updates)]"
     * </code>
     *
     * @author Adapted from Burak Guzel <http://net.tutsplus.com/tutorials/php/6-codeigniter-hacks-for-the-masters/>
     *
     * @param mixed $value  The value to be checked.
     * @param mixed $params The table and field to check against, if a second
     * field is passed in this is used as "AND NOT EQUAL".
     *
     * @return bool True if the value is unique for that field, else false.
     */
    public function unique($value, $params)
    {
        // Allow for more than 1 parameter.
        $fields = explode(',', $params);

        // Extract the table and field from the first parameter.
        list($table, $field) = explode('.', $fields[0], 2);

        // Setup the db request.
        $this->CI->db->select($field)
                     ->from($table)
                     ->where($field, $value)
                     ->limit(1);

        // Check whether a second parameter was passed to be used as an
        // "AND NOT EQUAL" where clause
        // eg "select * from users where users.name='test' AND users.id != 4
        if (isset($fields[1])) {
            // Extract the table and field from the second parameter
            list($where_table, $where_field) = explode('.', $fields[1], 2);

            // Get the value from the post's $where_field. If the value is set,
            // add "AND NOT EQUAL" where clause.
            $where_value = $this->CI->input->post($where_field);
            if (isset($where_value)) {
                $this->CI->db->where("{$where_table}.{$where_field} <>", $where_value);
            }
        }

        // If any rows are returned from the database, validation fails
        $query = $this->CI->db->get();
        if ($query->row()) {
            //$this->CI->form_validation->set_message('unique', lang('bf_form_unique'));
            $this->CI->form_validation->set_message('unique', $this->CI->lang->line('form_validation_is_unique'));
            return false;
        }

        return true;
    }
Reply




Theme © iAndrew 2016 - Forum software by © MyBB