Part of the EllisLab Network
   
 
Validation Bug
Posted: 01 September 2007 12:59 PM   [ Ignore ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  636
Joined  09-01-2007

I was writing a new form for my website, when I needed to check first if a date was in the correct format, then if it was not in the past so I created the following code:

$rules['expire'] = 'callback_dateformat|callback_dateinpast';        

function
dateformat($str)
{
  $this
->validation->set_message('dateformat','The %s is not in the format DD/MM/YYYY');
  return (
ereg('[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}',$str)) ? TRUE : FALSE;
}

function dateinpast($str)
{
  $this
->validation->set_message('dateinpast','The %s cannot fall in the past');
  list(
$day,$month,$year) = explode('/',$str);                 
  
$unixtime = mktime(0,0,0,$month,$day,$year);        
  return (
$unixtime>=time()) ? TRUE : FALSE;    
}

(Of course this isn’t how my file is structured)

So now if I don’t enter a correct date I get the first error, but if I enter correct date but one in the past I don’t get an error. Now I know were the problem is. If I have the following field rule.

$rules['expire'] = 'required|callback_dateformat|callback_dateinpast';

It will work, since in Validation.php Line 279 it only moves on if the result is TRUE and its required. Otherwise it cuts out. Now its fine when only one callback is used but with two it breaks.

 Signature 

Kaydoo - A day in the life of a developer
BackendPro Control Panel

Profile
 
 
Posted: 02 September 2007 11:17 AM   [ Ignore ]   [ # 1 ]  
Grad Student
Avatar
Rank
Total Posts:  72
Joined  06-06-2007

Hmmm, i thought we were only able to use one custom validation callback per field rule.

 Signature 

Mac Pro 4x3ghz, 1.8TB HD, 4G RAM, Dual 20” Screens in the house ... *drool*

Profile
 
 
Posted: 02 September 2007 12:04 PM   [ Ignore ]   [ # 2 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  636
Joined  09-01-2007

I didn’t know we are only allowed one callback, that sounds a bit silly to me. So if you are then surely the above shouldn’t be allowed. Seems to be a problem somewhere.

 Signature 

Kaydoo - A day in the life of a developer
BackendPro Control Panel

Profile
 
 
Posted: 02 September 2007 12:42 PM   [ Ignore ]   [ # 3 ]  
Grad Student
Avatar
Rank
Total Posts:  72
Joined  06-06-2007

I believe thats one of the reasons people usually extend the validation class, one of those things you find out the hardway about CI ... it does some things absolutely great! and others not so much.

 Signature 

Mac Pro 4x3ghz, 1.8TB HD, 4G RAM, Dual 20” Screens in the house ... *drool*

Profile
 
 
Posted: 03 September 2007 05:38 PM   [ Ignore ]   [ # 4 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  141
Joined  03-19-2007

Why not use a single callback?  Does it have to be two?

$rules['expire'] = 'callback_date_valid';

function
date_valid($str)
{
  $msg
= '';
  
$flag = true;
  if (!
ereg('[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}',$str))
  
{
    $msg
.= 'The %s is not in the format DD/MM/YYYY';
    
$flag = false;
  
}
  
list($day,$month,$year) = explode('/',$str);                 
  
$unixtime = mktime(0,0,0,$month,$day,$year);
  if (
$unixtime < time())
  
{
    $msg
.= 'The %s cannot fall in the past';
    
$flag = false;
  
}
  $this
->validation->set_message('date_valid',$msg);
  return
$flag;
}

I just threw that together, but you get the idea.

Jon

 Signature 

$me = null;

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 719, on June 06, 2008 10:16 AM
Total Registered Members: 77571 Total Logged-in Users: 21
Total Topics: 101562 Total Anonymous Users: 2
Total Replies: 544411 Total Guests: 194
Total Posts: 645973    
Members ( View Memberlist )
Newest Members:  Idril616tonybernardcarterstarksColeJLinskitnealsemperjrawhallshiusbozzlynobluff