Welcome Guest, Not a member yet? Register   Sign In
Custom Form Validation nightmare
#1

[eluser]Davcon[/eluser]
Hi Folks,

I've just spent six hours trying to figure out form validation with Codeigniter. I've read everything that I can possibly find on the subject and I'm very depressed.

I can't even begin to stress how miserable this whole problem has made me. I'm beginning to wish I hadn't bothered with Codeigntier.

START

* I'm trying to build a user registration form on Codeigniter for a website.

* One of the fields on the database is date_joined. This will be a timestamp: int(11).

* The make the form user friendly, I've built three drop downs (day, month and year) so that the site user can easily submit a meaningful join date.

* To make form validation possible I have to make sure my three submitted drop downs equate to a meaningful date (for example, NOT 31st February 2010).

NO PROBLEMS SO FAR

* I then have to get Codeigniter to check to see if the submitted date was valid and produce a custom error message ("you did not select a valid join date") if the user screws up.

That's it.

Sounds easy huh?

Nightmare nightmare nightmare!!!

The callback function as described on the so-called User Guide does not work as it should. Also, because I'm checking the result from a calculation brought about by evaluating three form variables I think I have Code Igniter stumped. Codeigniter's form checking abilities are geared towards looking at each submitted variable, in isolation, one at a time.

END



Any general guidance words of advice would be really appreciated.
#2

[eluser]K-Fella[/eluser]
Can I ask... why are you asking the user to enter the join date?
#3

[eluser]Davcon[/eluser]
Please don't ask me that.

I'm trying to solve a problem that I'm having with Codeigniter. I don't want to cloud the issue with a discussion about why I'm giving the site admin that functionality. Cheers.
#4

[eluser]Dennis Rasmussen[/eluser]
Use the form validation as usual.
If that validates then do your own if-statements to check whether the date is valid.
If not then print out an error like usual.

Makes sense?
If not, shout!
#5

[eluser]Dennis Rasmussen[/eluser]
You can also create a callback function which validates the $_POST data from the 3 fields to check if its a valid date.

That's what the matches[field] callback does in the core file.
#6

[eluser]Davcon[/eluser]
Hi,

Many thanks for your reply. The problem I'm having seems to be at a really basic level. The Callback facility, as described in the user guide* simply does not work. It does nothing and has no effect whatsoever on the script.


Here's a very simplified version of the code I'm using...

Code:
function submit() {
    $this->load->library('form_validation');
    $this->form_validation->set_rules('username', 'Username', 'callback_username_check');
    
    
        if ($this->form_validation->run() == FALSE)
        {
            //form submission error - reload the form...
            $this->load->view('addmember');
        }
        else
        {
            //the form was filled out correctly
            print "well done";
            die();        
        }
        
        
        
        
    function username_check($str)
    {
        if ($str == 'test')
        {
            $this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }
        

    
}



http://ellislab.com/codeigniter/user-gui...#callbacks *
#7

[eluser]Dennis Rasmussen[/eluser]
Which version of CI are you using?
And exactly what isn't working?

You just posted the UserGuide example.
Is that what you're using in your own script?
#8

[eluser]Davcon[/eluser]
I'm using 1.7 bla (the latest version, downloaded two nights ago) and I'm glad you noticed.

It doesn't seem to matter what I put in that username_check function. It has no effect whatsoever on the script. Even if I just make the function:

Code:
function username_check($str)
    {
        print "hello";
        die();
    }



....it still has no effect whatsoever.

I'm beginning to think the user guide is flawed.
#9

[eluser]Davcon[/eluser]
...and to answer your question in as precise a manner as I possibly can, the problem is that the callback scripts are doing nothing. They are not being read and my form submissions are always successfull - regardless of what rules I put in the callback script.
#10

[eluser]Davcon[/eluser]
I solved it.

Although the code was good, I was trying to do everything from one controller. That seems to have somehow confused Codeigniter. Once I copied the user manual very strictly then the callback started to work.


This problem nearly killed me.

I even considered ditching Codeigniter and started investigating how other frameworks handle form validation.

I really hope that someone from the CI community can write a really clear explanation of how to carry out form validation. The explanations that I've read so far are a little bit muddy. If nobody else does this then I will.

Thanks for your help.




Theme © iAndrew 2016 - Forum software by © MyBB