Welcome Guest, Not a member yet? Register   Sign In
Validation Bug
#1

[eluser]adamp1[/eluser]
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:
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.

Code:
$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.
#2

[eluser]CodeOfficer[/eluser]
Hmmm, i thought we were only able to use one custom validation callback per field rule.
#3

[eluser]adamp1[/eluser]
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.
#4

[eluser]CodeOfficer[/eluser]
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.
#5

[eluser]JayTee[/eluser]
Why not use a single callback? Does it have to be two?
Code:
$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




Theme © iAndrew 2016 - Forum software by © MyBB