Welcome Guest, Not a member yet? Register   Sign In
Adding Functions to form_validation.php
#1

[eluser]Wackton[/eluser]
I'm working on a little project. I'm a total noob in PHP, that's why I chose CI. It's very nice and well documented.

- I've been reading the Form Validation article, and I decided to use a separate file called "form_validation" to process my form's rules. However, I want to add a callback for my email field. I tried duplicating it just the like username callback, but it doesn't work. Furthermore, since I decided to use the form_validation.php file, I cannot figure out how to add the callback function to it.

Basically put, how do I make a callback function to disallow the word "hotmail" in my email field?

Here's my form_validation.php code:
Code:
<?php
$config = array(
               array(
                     'field'   => 'username',
                     'label'   => 'Username',
                     'rules'   => 'trim|required|min_length[5]|max_length[20]|xss_clean'
                  ),
               array(
                     'field'   => 'password',
                     'label'   => 'Password',
                     'rules'   => 'trim|required|matches[passconf]|min_length[10]|max_length[25]|md5'
                  ),
               array(
                     'field'   => 'passconf',
                     'label'   => 'Password Confirmation',
                     'rules'   => 'trim|required|matches[passconf]'
                  ),  
               array(
                     'field'   => 'email',
                     'label'   => 'Email',
                     'rules'   => 'trim|required|valid_email'
                  )
            );
        
            ?>

How would I add:
Code:
Function email_check($str)
    {
        if ($str == 'hotmail')
        {
            $this->form_validation->set_message('email_check', 'The %s field can not be the word "hotmail"');

to the validation file? Thanks in advance, I really like CI!!

Here's my form.php file in case you need it:
Code:
<?php

class Form extends Controller {
    
    function index()
    {
        $this->load->helper(array('form', 'url'));
        
        $this->load->library('form_validation');
                    
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('myform');
        }
        else
        {
            $this->load->view('formsuccess');
        }
        
    }
}
?>

It someone could led me a virtual hand, I'd be very grateful. Thanks again..
#2

[eluser]smilie[/eluser]
Hi and welcome to CI :-)

Oke, if you need 'special' checks on your form (which do not already exist within CI) you will have to use so called callback functions.

It goes like:

Controller:
Code:
<?php
$config = array(
               array(
                     'field'   => 'username',
                     'label'   => 'Username',
                     'rules'   => 'trim|required|min_length[5]|max_length[20]|xss_clean'
                  ),
               array(
                     'field'   => 'password',
                     'label'   => 'Password',
                     'rules'   => 'trim|required|matches[passconf]|min_length[10]|max_length[25]|md5'
                  ),
               array(
                     'field'   => 'passconf',
                     'label'   => 'Password Confirmation',
                     'rules'   => 'trim|required|matches[passconf]'
                  ),  
               array(
                     'field'   => 'email',
                     'label'   => 'Email',
                     # Note here, that rule is pointing to callback_FUNCTIONAME
                     'rules'   => 'callback__email_check' # also note, there are 2 _ (underscores); one is for the callback_ and another to make _email_check private function
                  )
            );

    function _email_check() # No need for value in here; note _ before function name
    {
        if ($this->input->post('email') == 'hotmail') # You 'pull' e-mail value from $this->input->post();
        {
            $this->form_validation->set_message('email_check', 'The %s field can not be the word "hotmail"');
        }
        else
        {
            return TRUE; # You must tell in here that check is TRUE (ok);
        }
     }
?>

I am not sure if function _email_check can be in other file, I have always only used it in the same controller where the form validation is.

Also, be aware that now that you are calling your own function, you will also have to do checks that field is not empty, that e-mail address is a valid e-mail address and so on...

Good luck!
Smilie
#3

[eluser]Wackton[/eluser]
Thank you very much for your reply - I'll try it right now. I've also been messing around with tank_auth, it seems to have all the security areas coded pretty good.
#4

[eluser]Wackton[/eluser]
Hello, I applied the code and kept getting an error. I found out the controller is missing some code such as
Code:
class Form extends Controller {

function index()
    {
        $this->load->helper(array('form', 'url'));

but whenever I apply that code, it gives me errors.
#5

[eluser]SitesByJoe[/eluser]
Please repost your entire controller so we can see what's really going on.

What you posted is all wrong as it is.




Theme © iAndrew 2016 - Forum software by © MyBB