Solved

Codeigniter - custom form validation...

Posted on 2013-11-20
6
874 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…

739 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