Welcome Guest, Not a member yet? Register   Sign In
Form Valadation
#1

[eluser]R_Nelson[/eluser]
I have a simple Registration script that takes in first_name, last_name, email, username, & Password/password_confirm. The problem i have right now is say i use "test" as username and [email protected] for email they both pass the validation but i get an error from the DB because they are suppose to be unique because there already in the DB. I can fix this with simple DB query's but how do i pass the errors along so they show up when i use
Code:
echo validation_errors();
at the top of my form?
#2

[eluser]bubbafoley[/eluser]
you can create a custom rule with a callback function

http://ellislab.com/codeigniter/user-gui...#callbacks
#3

[eluser]R_Nelson[/eluser]
just want to make sure i am reading this right all i need to do is add
Code:
callback_username_check
to
Code:
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]');
and then make my check for username in the DB username_check() function?

if its really that easy i really need to start reading more lol thx
#4

[eluser]R_Nelson[/eluser]
i found an even better way to do this by extending the form_validation class
Code:
class MY_Form_validation extends CI_Form_validation {

    function __construct()
    {
        parent::__construct();
    }

    // --------------------------------------------------------------------

    /**
     * Unique
     *
     * @access    public
     * @param    string
     * @param    field
     * @return    bool
     */
    function unique($str, $field)
    {
        $CI =& get_instance();
        list($table, $column) = explode('.', $field, 2);

        $CI->form_validation->set_message('unique', 'The %s that you requested is unavailable.');

        $query = $CI->db->query("SELECT COUNT(*) AS dupe FROM $table WHERE $column = '$str'");
        $row = $query->row();
        return ($row->dupe > 0) ? FALSE : TRUE;
    }
}
?>

so i just change my validation string to
Code:
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]|unique[user.username]');
where user is the DB name and username is whats being validated

thank you very much for the help i doubt i would have found this without knowing about the callback!
#5

[eluser]JonoB[/eluser]
You may want to check if the user is updating or inserting a record. If updating some fields in a record (and the username field is not being updated), then a dupe will be incorrectly found.
#6

[eluser]R_Nelson[/eluser]
Don't really expect the site to be that popular but just in case how do i do that?
#7

[eluser]JonoB[/eluser]
[quote author="R_Nelson" date="1300476266"]Don't really expect the site to be that popular but just in case how do i do that?[/quote]
I assume that you will either have a hidden form field, or store the user.id in the session. So, when you run your database validation to check for dupes, do something like:

Code:
//$id is the primary key for the table. If it is greater than zero, then it means that the record already exists
if ($id > 0)
{
    //updating an existing record, so check all records except the current record
    $sql = 'SELECT COUNT(*) as dupe from $table WHERE $column = '$str' AND id <>' . (int)$id;
}
else
{
    //inserting a new user, so check all records
    $sql = 'SELECT COUNT(*) as dupe from $table WHERE $column = 'str';
}




Theme © iAndrew 2016 - Forum software by © MyBB