Welcome Guest, Not a member yet? Register   Sign In
Adding field to validation_error() after database call
#1

[eluser]Unknown[/eluser]
Hey all. I would greatly appreciate some insight here. I am doing a signup form and I am using the form_validation library to output errors on the page. On the form I am checking to see if the email address already exists in the database, which is working fine, but how in the world do I add this to the error rules that are displayed after the database call has run? So lets say the email exists, I want to display "Sorry this email is already registered" in the validation_error() function. I hope that this makes sense. Any help would be greatly appreciated.
#2

[eluser]R_Nelson[/eluser]
i have My_form_validation.php file in libraries dir that looks like this
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* MY_Form_validation Class
*
* Extends Form_Validation library
*
* Adds one validation rule, "unique" and accepts a
* parameter, the name of the table and column that
* you are checking, specified in the forum table.column
*
* Note that this update should be used with the
* form_validation library introduced in CI 1.7.0
*/
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;
    }
}
?>
this works for me and i believe this is what your asking for!
you need to add unique[user.username]to your validation user is the name of the database and username is the row!
#3

[eluser]Unknown[/eluser]
Thank you for the reply my friend. I think I'm a little too noob to understand your file completely but I will study it. Thank you for the help.
#4

[eluser]R_Nelson[/eluser]
in a nut shell your extending the form validation library to a unigue as a validation




Theme © iAndrew 2016 - Forum software by © MyBB