Solved

Codeigniter - custom form validation...

Posted on 2013-11-20
6
893 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 110

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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
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

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

705 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