?
Solved

Codeigniter - custom form validation...

Posted on 2013-11-20
6
Medium Priority
?
928 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 111

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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 28

Accepted Solution

by:
FishMonger earned 2000 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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
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 fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses

777 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