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.
P.S. Though I guess it's not relevant, I'm using V4 with Joomla 1.6
$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
Hi dmontpe,
Please try this:
Bob
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
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:
and it works just fine. But then I even replaced it with this isolated part of your code:
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.
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.
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
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
This topic is locked and no more replies can be posted.