Welcome Guest, Not a member yet? Register   Sign In
Updating a unique field in database (example: account username or email)
#3

[eluser]Salvatore Formisano[/eluser]
Thanks for the reply mate.

I don't know if my setup might work out with this solution, I currently have:

a config array here:

Code:
/application/config/form_validation.php

the array for the specific case I described is:

Code:
$config = array(
    'user_update_account_parser' => array(
        array(
            'field' => 'id',
            'label' => 'id',
            'rules' => 'required|integer'
            ),
        array(
            'field' => 'first_name',
            'label' => 'Nome',
            'rules' => 'required|min_length[4]|max_length[32]|alpha_dash'
            ),
        array(
            'field' => 'last_name',
            'label' => 'Cognome',
            'rules' => 'required|min_length[4]|max_length[32]|alpha_dash'
        ),
        array(
            'field' => 'username',
            'label' => 'Username',
            'rules' => 'required|min_length[6]|max_length[32]|alpha_dash|unique[accounts.username]'
        ),
        array(
            'field' => 'email',
            'label' => 'Email',
            'rules' => 'required|valid_email|unique[accounts.email]'
        ),
        array(
            'field' => 'new_password',
            'label' => 'Nuova Password',
            'rules' => 'matches[new_password_confirm]|min_length[6]|max_length[32]'
        ),
        array(
            'field' => 'new_password_confirm',
            'label' => 'Conferma Nuova Password',
            'rules' => 'matches[new_password]|min_length[6]|max_length[32]'
        )
    )
);

the unique rule (unique[table.column) uses an extension of the CI_Form_validation class. Here's the code

Code:
class MY_Form_validation extends CI_Form_validation {

    function My_Form_validation($rules = array())
    {
        parent::CI_Form_validation($rules);
    }
    
    // --------------------------------------------------------------------
    
    /**
     * Unique
     *
     * @access    public
     * @param    string
     * @param    field
     * @return    bool
     */
    
    // this function will make sure the value is not already in the database (you can specify table and column)
    function unique($str, $field)
    {
        $CI =& get_instance();
        list($table, $column) = split("\.", $field, 2);
        $CI->form_validation->set_message('unique', 'The %s that you requested is unavailable.');
        $query = $CI->db->query("SELECT COUNT(*) dupe FROM $table WHERE $column = '$str'");
        $row = $query->row();
        return ($row->dupe > 0) ? FALSE : TRUE;
    }
}


so basically... if I want to duplicate the same unique function calling it, let's say, unique_update... how would I manage to bring the id of the record to be excluded here safely?


I am not an expert in back-end, and I want to make sure what I do is not risky.

Thanks again,
Salvatore


Messages In This Thread
Updating a unique field in database (example: account username or email) - by El Forum - 12-10-2009, 07:07 PM



Theme © iAndrew 2016 - Forum software by © MyBB