CodeIgniter Forums
Validation help required - how can I trigger a validation message from my code... - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Validation help required - how can I trigger a validation message from my code... (/showthread.php?tid=59812)



Validation help required - how can I trigger a validation message from my code... - El Forum - 11-22-2013

[eluser]CrisThompson[/eluser]
Hi,

I'm working on my first CI project, although have tons of PHP experience.

I have a form capturing a date or birth as 3 separate fields, and in the form validation, I need to convert these 3 fields to a timestamp, make sure the user is older that 18, and then pass back a fail validation message if they are not.

I can do the date manipulation, but how do I trigger a validation message from my php - it's based on 3 fields not 1, so how does callback work?

Cris.


Validation help required - how can I trigger a validation message from my code... - El Forum - 11-22-2013

[eluser]Otemu[/eluser]
Hi,

Validation can be called on any field, for instance if you have separate input for day, month and year, you could have the callback set on year

Code:
$this->form_validation->set_rules('year', 'year', 'callback_date_check');

public function date_check($str)
{
  //then you can write any logic you like here to check for valid date, grabbing all three fields
  //$day = $this->input->post('day');
  //$month = $this->input->post('month');
  //$year = $this->input->post('year');
  
  //check if date valid
  if (logic for date)
  {
   //since this will check all three fields, doesn't matter that there seperate
   //set message return false
   $this->form_validation->set_message('invalid date');
   return FALSE;
  }
  else
  {
   //validation passed
   return TRUE;
  }
}

Hope that helps


Validation help required - how can I trigger a validation message from my code... - El Forum - 11-22-2013

[eluser]CroNiX[/eluser]
The first parameter of set_message must be the same name as the validation method it's being set in or the validator won't be able to retrieve the error message for whatever field it was assigned to, so in this case it would be $this->set_message('date_check').

Cris, I don't think you should convert it to a timestamp in the validation. You should only check whether the supplied date was valid, and that the person was over 18. If the form passes validation (meaning the date was valid along with all other fields), your model should be the one to convert it to a timestamp before it inserts/updates into the db.

Code:
//grab year, month and days fields
$y = $this->input->post('year');
$m = $this->input->post('month');
$d = $this->input->post('day');

//Is the date a valid date?
if ( ! checkdate($y, $m, $d))
{
  //set message for invalid date
  $this->form_validation->set_message('date_check', 'Birth Date', 'The date you supplied does not seem to be valid.');
  return FALSE;
}

//date is valid, check for at least 18 years of age
$birthdate = new DateTime("$y-$m-$d");
$today = new DateTime();
$interval = $today->diff($birthdate);
$years_old = $interval->format('%y');
if ($years_old < 18)
{
  //set message for under 18
  $this->form_validation->set_message('date_check', 'Birth Date', 'We are sorry; you are under 18 years old.');
  return FALSE;
}

//passed
return TRUE;



Validation help required - how can I trigger a validation message from my code... - El Forum - 11-25-2013

[eluser]CrisThompson[/eluser]
Thanks for your replies...

All sorted.