Welcome Guest, Not a member yet? Register   Sign In
My extended form validation class wont work
#1

[eluser]phpserver[/eluser]
I want to check whether an email that has been submitted is already in my database.Here is my contoller::

form.php

Code:
<?php

class Form extends Controller {

function Form()
    {
        parent::Controller();
        $this->load->model('arc_model');
    }
    
    function index()
    {
        $this->load->helper(array('form', 'url'));
        $this->load->library('Validation');
        $this->load->library('form_validation');
            
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[12]|unique[mytable.username]|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|matches[passconf]|md5');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required');
$this->form_validation->set_rules('email', 'Email', 'trim|required|unique[mytable.email]|valid_email');
            
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('myform');
        }
        else
        {
        $this->arc_model->insert($this->input->post('username'), $this->input->post('email'));
                redirect('form/success');
        }
    }
function success()
{
echo "Your data has been securely submitted and is now in our safe custody";
}
}
?>

Here is MY_Validation.php class

MY_Validation.php

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* MY_Validation Class
*
* Extends 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
*/
class MY_Validation extends CI_Validation {

    function My_Validation()
    {
        parent::CI_Validation();
    }

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

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

        $CI->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;
    }
}
?>
#2

[eluser]Pascal Kriete[/eluser]
You're extending validation (deprecated), but you're using form_validation. One of them will need to change.
#3

[eluser]xwero[/eluser]
Validation and form validation are two different validation classes and you are mixing them up. The controller has form validation code and the class you extended is the validation class.

The unique method doesn't need a $CI variable because it's defined in the parent class already. And if you want to call a method from the parent class you can do it with $this-> because of the inheritance.




Theme © iAndrew 2016 - Forum software by © MyBB