Welcome Guest, Not a member yet? Register   Sign In
Form Validation Callbacks Load From External Source
#1

[eluser]skunkbad[/eluser]
I've got more than one controller that calls some of the identical callback functions to validate form data using the CI Form Validation class. I'm wondering what I can do to get them out of the controller, and into a centralized form validation callback library, plugin, helper, or whatever. The docs don't mention loading the callbacks from an external source, and I'd be happy if it is possible. What are other people doing to make this happen?

Right now I'm trying to test loading one of the callback functions from the controllers constructor. So, for instance:

Code:
function Administration()
{
    parent::Controller();
    $this->load->plugin('usernamecheckoff');
}

Then, elsewhere in the controller, when I'm trying to use the function as a callback for form validation:

Code:
$this->form_validation->set_rules('uname', 'USERNAME' , 'trim|required|max_length[12]|callback_usernamecheckoff');

and then I have the actual plugin, appropriately placed in system/application/plugins/ and named usernamecheckoff_pi.php:

Code:
<?php
function usernamecheckoff($uname){
    if($uname != 'billythekid'){
        $this->form_validation->set_message('usernamecheckoff', 'Supplied <span class="redfield">%s</span> was not billythekid.');
        return FALSE;
    }else{
        return $uname;
    }
}

But this isn't working. If for instance, in my form I use a name other than 'billythekid', the form validation error message is not displayed.
#2

[eluser]janogarcia[/eluser]
Take a look at MY Form validation - Simple Callbacks into Models, it will let you specify where to locate each callback: The current Controller or a User specified Model. It defaults to the current Controller, but you can override that for each callback.
#3

[eluser]skunkbad[/eluser]
OK, so I figured out how to make for external form validation callbacks. I'm not sure this is the best way, but it's what works. The key for my success was to use a model, in which I can store all the different callbacks as methods. I know to a MVC purist, I am probably committing the ultimate sin using a model for this purpose, but like I said before, it is what works. Should I have made a library? Well, maybe so. Anyways...

The form validation rule is set as normal, then inside the controller I put a method/function like this:

Code:
public function _usernameChecks($uname){
    list($result , $error_string) = $this->data_checks->username($uname);
    if($result === FALSE){
        $this->form_validation->set_message('_usernameChecks', $error_string);
        return FALSE;
    }else{
        return $result;
    }
}

And the model, which currently only has one method (because it took me so long to figure out what to do to make this work) looks like this:

Code:
&lt;?php
class Data_checks extends Model {

    function username($uname){
        if(strlen($uname) < MIN_CHARS_4_USERNAME){
            $response[] = FALSE;
            $response[] = 'Supplied <span class="redfield">%s</span> was too short.';
            return $response;
        }else{
            //Check against user table
            $user_table = $this->authentication->get_table('user_table');
            $this->db->where('user_name', $uname);
            $query = $this->db->get_where($user_table);
            if ($query->num_rows() > 0){ //if user name already exists
                $response[] = FALSE;
                $response[] = 'Supplied <span class="redfield">%s</span> already exists.';
                $query->free_result();
                return $response;
            }else{
                $response[] = $uname;
                $response[] = '';
                $query->free_result();
                return $response;
            }
        }
    }

}

If anyone has any other ideas, let me know.
#4

[eluser]skunkbad[/eluser]
Support this kind of functionality by voting at uservoice!

http://codeigniter.uservoice.com/forums/...?ref=title
#5

[eluser]InsiteFX[/eluser]
@skunkbad

I have a auth library were I place all my forms and callbacks into this library

Then I add a special view method to display them.

Everything in one nice place.

InsiteFX
#6

[eluser]janogarcia[/eluser]
Just added my vote. I can't understand why this hasn't been implemented yet.




Theme © iAndrew 2016 - Forum software by © MyBB