Part of the EllisLab Network
   
 
Validate Drop Down Selections
Posted: 01 September 2010 10:57 AM   [ Ignore ]  
Grad Student
Rank
Total Posts:  33
Joined  05-31-2010

Hi,

I want to validate a date formed with drop down selections. I have three different drop down menus. (Year, Month, Day)
When it is submitted the controller receives three different POSTS. One for each drop down.

I can make the validation very easy with PHP checkdate() function:

$y $this->input->post('y');
$m $this->input->post('m');
$d $this->input->post('d');
if(
checkdate($m$d$y)) {
     
return true;
else {
     
return false;

but if it fails how can I display the custom error message?

Profile
 
 
Posted: 01 September 2010 12:55 PM   [ Ignore ]   [ # 1 ]  
Grad Student
Rank
Total Posts:  46
Joined  06-01-2010

I would make 1 field to input the date.

If you want to use CI’s formvalidation class you can do something like this:

[CODE]
$this->form_validation->set_rules(‘date’, ‘Date’, ‘callback_date_check’);
[/CODE]

[CODE]
function date_check ($value)
{
  $parts = explode(’/’, $value);
 
  // make sure that all parts are digits. otherwise the checkdate method will throw errors
 
  if ( count($parts) != 3 || !checkdate($parts[1]  ,$parts[0]  ,$parts[2]) )
  {
      $this->load->library(‘validation’);
      $this->validation->set_message(__FUNCTION__, ‘The %s field must have dd/mm/yyyy format.’);
      return FALSE; 
  }
     
  return true;
}
[/CODE]

Profile
 
 
Posted: 01 September 2010 01:00 PM   [ Ignore ]   [ # 2 ]  
Grad Student
Rank
Total Posts:  33
Joined  05-31-2010

That’s the thing. I don’t want to use a text input field. I would like to have the drop downs… every one is using drop downs.

Profile
 
 
Posted: 01 September 2010 01:14 PM   [ Ignore ]   [ # 3 ]  
Grad Student
Rank
Total Posts:  46
Joined  06-01-2010

The date check finetuned:
[CODE]
function date_check ($value)
{
  //match the format of the date
  if ( preg_match (”#^([0-9]{2})/([0-9]{2})/([0-9]{4})$#”, $value, $parts) )
  {       
      //check weather the date is valid of not
      if( checkdate($parts[2],$parts[1],$parts[3]) )
      {
        return true;
      }
  }
         
  $this->load->library(‘validation’);
  $this->validation->set_message(__FUNCTION__, ‘The %s field must have dd/mm/yyyy format.’);
  return FALSE; 
}
[/CODE]

Profile
 
 
Posted: 01 September 2010 01:20 PM   [ Ignore ]   [ # 4 ]  
Grad Student
Rank
Total Posts:  46
Joined  06-01-2010
Petsoukos - 01 September 2010 05:00 PM

That’s the thing. I don’t want to use a text input field. I would like to have the drop downs… every one is using drop downs.

I don’t agree about that wink

I notice that the dropdowns are more and more replaced by textfields together with a datepicker. is more userfriendly (only one click)

jquery datepicker: http://www.kelvinluck.com/assets/jquery/datePicker/v2/demo/

Profile
 
 
Posted: 01 September 2010 01:26 PM   [ Ignore ]   [ # 5 ]  
Grad Student
Rank
Total Posts:  46
Joined  06-01-2010

or:

[CODE]
// run the validation on 1 of the dropdowns
$this->form_validation->set_rules(‘d’, ‘Date’, ‘callback_date_check’); 
[/CODE]

[CODE]
function date_check ($value)
{
  $y = $this->input->post(‘y’);
  $m = $this->input->post(‘m’);
  $d = $this->input->post(‘d’);

  if( checkdate($m,$d,$y) )
  {
      return true;
  }
         
  $this->load->library(‘validation’);
  $this->validation->set_message(__FUNCTION__, ‘The %s field is not a valid date.’);
  return FALSE; 

[/CODE]

Profile
 
 
Posted: 01 September 2010 01:42 PM   [ Ignore ]   [ # 6 ]  
Grad Student
Rank
Total Posts:  33
Joined  05-31-2010

I tried the jQuery datepicker but for some odd reason when I click on the text field it just displays it in the main body of the page and not as a “pop-out” frame. Can’t figure out how to make it work.

Profile
 
 
Posted: 01 September 2010 01:50 PM   [ Ignore ]   [ # 7 ]  
Grad Student
Rank
Total Posts:  46
Joined  06-01-2010

You have to include the css:

http://www.kelvinluck.com/assets/jquery/datePicker/v2/demo/styles/datePicker.css

Profile
 
 
Posted: 01 September 2010 01:59 PM   [ Ignore ]   [ # 8 ]  
Grad Student
Rank
Total Posts:  33
Joined  05-31-2010

I didn’t forget…

<link rel="stylesheet" href="<?php echo base_url();?>application/views/templates/default/css/datePicker.css" type="text/css" media="screen" charset="utf-8" />
script type="text/javascript" src="<?php echo base_url();?>application/views/templates/default/js/jquery.1.4.2.js">[removed]
script type
="text/javascript" src="<?php echo base_url();?>application/views/templates/default/js/date.js">[removed]
script type
="text/javascript" src="<?php echo base_url();?>application/views/templates/default/js/jquery.datePicker.js">[removed] 

This is the code from my header.

Maybe the CSS file has to be on a different location…

Profile
 
 
Posted: 01 September 2010 03:21 PM   [ Ignore ]   [ # 9 ]  
Grad Student
Rank
Total Posts:  46
Joined  06-01-2010

Does the link to your css file work?
Try view source and copy paste the css link in your browser.

Profile
 
 
Posted: 01 September 2010 03:23 PM   [ Ignore ]   [ # 10 ]  
Grad Student
Rank
Total Posts:  33
Joined  05-31-2010
iConTM - 01 September 2010 07:21 PM

Does the link to your css file work?
Try view source and copy paste the css link in your browser.

Yes it spits out the script…

Profile
 
 
Posted: 01 September 2010 03:35 PM   [ Ignore ]   [ # 11 ]  
Grad Student
Rank
Total Posts:  46
Joined  06-01-2010

Ah maybe it’s the version of jquery?

I used jquery-1.3.2.min.js

I noticed that some jquery plugins only work with a certain version of jquery installed.

Profile
 
 
Posted: 01 September 2010 06:06 PM   [ Ignore ]   [ # 12 ]  
Grad Student
Rank
Total Posts:  33
Joined  05-31-2010

I have the latest in use.

Profile
 
 
Posted: 01 September 2010 07:54 PM   [ Ignore ]   [ # 13 ]  
Grad Student
Rank
Total Posts:  46
Joined  06-01-2010

try using jquery-1.3.2.min.js

Profile
 
 
Posted: 02 September 2010 02:35 AM   [ Ignore ]   [ # 14 ]  
Grad Student
Rank
Total Posts:  33
Joined  05-31-2010

Well I mixed up the datepicker utilities. Actually I’ve used this http://javascriptcalendar.org/javascript-date-picker.php this was causing the problem I’ve mentioned.

The one you are referring is working. You said the textfield+datepicker is more userfriendly. One click only. But I think otherwise. With this date picker I have to click 25 times to get to my year of birth + 1 to select the date. wink

Profile
 
 
Posted: 02 September 2010 10:12 AM   [ Ignore ]   [ # 15 ]  
Grad Student
Rank
Total Posts:  33
Joined  05-31-2010

Hm… I’ve got it running with drop down and it validates correctly as far I can tell.

I check the validation from on point only

$this->form_validation->set_rules('year''Year''trim|required|numeric|callback_datechecker');
$this->form_validation->set_rules('month''Month''trim|required|numeric');
$this->form_validation->set_rules('day''Day''trim|required|numeric'); 

The form validation on the year input.

The callback method:

function DateChecker() {
$y 
$this->input->post('year');
$m $this->input->post('month');
$d $this->input->post('day');
    if(
checkdate($m$d$y)) {            
        
return true;
    
else {
        $this
->form_validation->set_message('datechecker''Invalid Date!');
        return 
false;
    
}

Can someone else confirm this? When for example I enter an erroneous date like 2010 02 31 (31 Feb tongue wink ) I get the error message. Or any invalid date. If someone was so kind to review it for any loopholes or a trick to bypass it I would be greatfull.

Thanks!

Profile