validation_errors['nac_fecha_a'] = 'La fecha es incorrecta. Debes escribir día, mes y año. Ej.: 05-09-1988'; $error_count++; }P.S. Though I guess it's not relevant, I'm using V4 with Joomla 1.6"> [SOLVED] What's wrong with this validation code? - Forums

Forums

[SOLVED] What's wrong with this validation code?

dmontpe 24 Jun, 2011
I am trying to validate a birthdate (provided in 3 separate inputs: day [nac_fecha_d], month [nac_fecha_m] and year [nac_fecha_a]), first to see if it's a valid date and then to see if the person is 18 or older. The problem is everytime I add this block of code to my custom validation, my form gets into event loops with no error message displayed. As hard as I look at this I can't find what's causing it. I'm no PHP expert, so any input will be appreciated.

 
  $trimmed_d = trim ($form->data{'nac_fecha_d']));
  $trimmed_m = trim ($form->data{'nac_fecha_m']));
  $trimmed_a = trim ($form->data{'nac_fecha_a']));  

  if ( checkdate($trimmed_m,$trimmed_d,$trimmed_a) )

  {
    //// Calcular edad ///////////////////////////  
    $a_dif = date("Y") - $trimmed_a;
    $m_dif = date("m") - $trimmed_m;
    $d_dif = date("d") - $trimmed_d;

    if (($d_dif < 0) || ($m_dif < 0))
	
    { $a_dif--; }

    if ( $a_dif < 18 )
	
    {
      $form->validation_errors['nac_fecha_a'] = 'Lo sentimos. Tienes que ser mayor de edad para inscribirte. Si escribiste una fecha de nacimiento equivocada, corrígela por favor.';
      $error_count++;
    }
  }

  else
  
  {
    $form->validation_errors['nac_fecha_a'] = 'La fecha es incorrecta. Debes escribir día, mes y año. Ej.: 05-09-1988';
    $error_count++;
  }


P.S. Though I guess it's not relevant, I'm using V4 with Joomla 1.6
GreyHead 26 Jun, 2011
Hi dmontpe,

Please try this:
<?php
$error_count = 0;
$d = trim ($form->data['nac_fecha_d']);
$m = trim ($form->data['nac_fecha_m']);
$a = trim ($form->data['nac_fecha_a']);
if ( checkdate($m, $d ,$a) ) { 
  $timestamp_18 = mktime(0, 0, 0, $d, $m, $a + 18);
  if ( $timestamp_18 > mktime() ) {
    $form->validation_errors['nac_fecha_a'] = 'Lo sentimos. Tienes que ser mayor de edad para inscribirte. Si escribiste una fecha de nacimiento equivocada, corrígela por favor.';
    $error_count++;
  }
} else {
  $form->validation_errors['nac_fecha_a'] = 'La fecha es incorrecta. Debes escribir día, mes y año. Ej.: 05-09-1988';
  $error_count++;
}
if ( $error_count ) {
  return false;
}
?>

Bob
dmontpe 27 Jun, 2011
Thanks, Greyhead. I tried the code and it didn't work either. Just in case, I hadn't added the first and last line(s) (concerning error_count) because the code was just part of a larger whole. I'm really confused because I already have this:

function is_digits($element) { return !preg_match ("/[^0-9]/", $element); }

if ( (is_digits($form->data['nac_fecha_d'])) && (is_digits($form->data['nac_fecha_m'])) && (is_digits($form->data['nac_fecha_a'])) && (strlen($form->data['nac_fecha_a']) == 4) ) { }

else {
  $form->validation_errors['nac_fecha_a'] = 'La fecha es incorrecta. Debes escribir día, mes y año. Ej.: 05-09-1988';
  $error_count++;
}

and it works just fine. But then I even replaced it with this isolated part of your code:

$d = trim ($form->data{'nac_fecha_d']);
$m = trim ($form->data{'nac_fecha_m']);
$a = trim ($form->data{'nac_fecha_a']); 
if ( checkdate($m, $d ,$a) ) { }
else {
  $form->validation_errors['nac_fecha_a'] = 'La fecha es incorrecta. Debes escribir día, mes y año. Ej.: 05-09-1988';
  $error_count++;
}

And it fails the same way: whether the input is valid or not, the form gets reloaded with no error message. (Notice it's the same error message that is defined) I just don't get what's failing.
dmontpe 29 Jun, 2011
Could this be a bug?

David
GreyHead 29 Jun, 2011
Hi David,

I built a test form and fixed a few typos ( four bad {s and a $d ) and the code now appears to work OK.

I've updated my previous post with the corrected version.

Bob
dmontpe 30 Jun, 2011
Thank you so much, Bob. It does work now. I'm sorry I gave up going over the code just too soon.
This topic is locked and no more replies can be posted.