Welcome Guest, Not a member yet? Register   Sign In
Would this work?
#1

[eluser]IamPrototype[/eluser]
Hey. I started up a project myself. I'd like to create a mini community (traning, training...). So far I've coded my auth library and auth model or at least some if it. I coded it in a way so my controller sends some data to the library and my library connects to my model and then back to my library and back to my controller. You see? Sorry, english isn't my native language. Tongue

What I'd like know is: Would form validation work that work?

Some code from my library, a function named register
Code:
function register($register = NULL)
    {
        if ($CI->Auth_model->process_register($register))
        {
            return TRUE;
        }
        else
        {
            return FALSE;
        }
    }

That function, register, will connect to my model.

Some code from my model, a process function named process_register
Code:
function process_register($register)
    {
        // Run a few safety checks
        // The array has to be set otherwise return false
        if ( ! isset($register))
            return FALSE;

        // The array has to have 4 values otherwise return false
        if (count($register) != 4)
            return FALSE;

        $username           = $register[0];
        $password           = $register[1];
        $password_conf      = $register[2];
        $email              = $register[3];

        $this->form_validation->set_rules('username', 'Username', 'required|min_length[4]|max_length[12]|callback_username_check');
        $this->form_validation->set_rules('password', 'Password', 'required|min_length[6]|max_length[18]|matches[password_conf]');
        $this->form_validation->set_rules('password_conf', 'Password Conf', 'required|min_length[6]|max_length[18]');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email|callback_email_check');

        if ($this->form_validation->run() == FALSE)
        {
            // The validation wasn't valid, return FALSE............ HOW CAN I RETURN VALIDATION ERRORS TO A VIEW?
            return FALSE;
        }
        else
        {
            // User has been registered successfully, return TRUE, I HAVEN'T DONE THE MYSQL STUFF YET FOR INSERTING
            return TRUE;
        }
    }

Then in my controller I'd do it like this.
Code:
if ($this->Auth->register($register_array))
{
// registration succeded
}
else
{
// registration failed
}

Would that work out? Or would my validation lose its validation errors because it's going through a library and a model? Just curious!
#2

[eluser]slowgary[/eluser]
Try it and see. Let us know while you're at it. Thanks.
#3

[eluser]IamPrototype[/eluser]
Just curious because form validation normally are placed in the controller, right?
#4

[eluser]Dam1an[/eluser]
Of the Authentication libraries I've seen, the validation takes place in the library, not the controller
#5

[eluser]TheFuzzy0ne[/eluser]
I don't think it will work, although it's certainly possible - http://codeigniter.com/wiki/MY_Validatio...to_Models/. Personally, I feel it makes more sense to keep the validation rules and the running of the validation process in the controller methods, not the model.
#6

[eluser]IamPrototype[/eluser]
I think it makes more sense to keep them in the controller too, oh well, I'll re-code. Smile
#7

[eluser]Thorpe Obazee[/eluser]
Others may say that validation doesn't belong in the controller Smile

I guess it depends on the coder :p
#8

[eluser]IamPrototype[/eluser]
Yes, but I guess it would be easiest to put it in the controller as I used to do. Tongue Just thought that the code I posted up there would be easier. Smile




Theme © iAndrew 2016 - Forum software by © MyBB