Solved

Codeigniter - custom form validation...

Posted on 2013-11-20
6
843 Views
Last Modified: 2013-11-28
Hi,

I have tried to follow a couple of posts about this, but either they are incomplete or don't work for me.

Here's my issue:
I am allowing a user to register and collect their date of birth in 3 separate fields.

I need to check the date entered to make sure they are over 18, and I would like to generate a standard validation error if not.  So this is not a rule I can attach to one field as it's 3 fields, that I will need to make into a timestamp and validate.

Can anyone help?

Cris.
0
Comment
Question by:CrisThompsonUK
  • 3
  • 2
6 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39663421
You may not need to collect three fields.  See practical application #1 in this article
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

Basically anything that is a valid date can be converted to a UNIX timestamp with PHP strtotime().  And once you have a timestamp you can compare it to time().

Example here: http://www.laprbass.com/RAY_strtotime.php?s=-18+years
0
 

Author Comment

by:CrisThompsonUK
ID: 39668372
Thanks Ray.

I'm familiar with all the PHP necessary to do the checking, I just need a codeigniter pointer to work out how to trigger a standard codeigniter validation error.

There don't seem to be any CI bods on here...

Cris.
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 39669618
You need to create a callback function to be used in the CI validation rule that validates the 3 fields and sets the custom error message if the validation fails.
Callbacks: Your own Validation Functions

You could either apply the callback function to all 3 fields, or just a single field since the function itself will handle the validation of all 3 fields.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 28

Accepted Solution

by:
FishMonger earned 500 total points
ID: 39669649
Here's an example from one of my controller scripts.
    $this->form_validation->set_rules('cd-key[0]', '1st CD-Key', 'callback_isValid_cd_key');
    $this->form_validation->set_rules('cd-key[1]', '2nd CD-Key', 'callback_isValid_cd_key');
    $this->form_validation->set_rules('cd-key[2]', '3rd CD-Key', 'callback_isValid_cd_key');
    $this->form_validation->set_rules('cd-key[3]', '4th CD-Key', 'callback_isValid_cd_key');
    $this->form_validation->set_rules('cd-key[4]', '5th CD-Key', 'callback_isValid_cd_key');

# further down in the script
    function isValid_cd_key($field)
    {
        if  ($_POST['OS'] == 'Windows' && preg_match('/[^a-z0-9]/i', $field) or strlen($field) != 5) {
            $this->form_validation->set_message('isValid_cd_key', 'The %s field must be exactly 5 characters long and only contain letters and numbers');
            return false;
        }
        return true;
    }

Open in new window

My function is only checking a single field, but it could just as easily check all 5 fields in a single call.
0
 

Author Closing Comment

by:CrisThompsonUK
ID: 39683219
Thanks Fishmonger.

This is what I did in the end, just attaching the callback to one field.

I think I was asking if you can trigger a validation error without attaching a callback to a field validation...

Is there much CI action here on EE, I have also recently been posting in to the CI forums...
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 39683658
I'm not sure how much CI questions come through.  

For the most part I focus on the Perl questions.  I really don't like working with PHP, but my company wants me to do all of my web coding in it simply because the others in our team don't know Perl.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

It’s a strangely common occurrence that when you send someone their login details for a system, they can’t get in. This article will help you understand why it happens, and what you can do about it.
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question