Solved

Codeigniter - custom form validation...

Posted on 2013-11-20
6
792 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 108

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now