Forums

LiveValidation

Tryt 04 Nov, 2009
hello,

I know this is a chronoforms forum, but since there is a liveValidation library used maybe you can help me:)

I have a field which need to be filled with one number from allowed, that's easy:

<?php
$script = "
  var wiek_1 = new LiveValidation('wiek_1');
  wiek_1.add(Validate.Presence, { failureMessage: 'Data urodzenia jest wymagana!' });
  wiek_1.add(Validate.Numericality, { minimum: 2003, maximum: 2007, failureMessage: 'Zła data urodzenia' });
";
$script = "
  window.addEvent('domready', function() {
  $script
  });
";
$doc =& JFactory::getDocument();
$doc->addScriptDeclaration($script);
?>


But the values will vary depending on the year i.e. in 2010 - { minimum: 2004, maximum: 2008 }, in 2011 - { minimum: 2005, maximum: 2009 } and so on. So i want it to be a little more dynamic:) I.ve found an sql function which would make it for me but unfortunately doesn't want to work:/

SELECT YEAR(DATE_SUB(CURDATE(), INTERVAL 6 YEAR));

and
SELECT YEAR(DATE_SUB(CURDATE(), INTERVAL 2 YEAR));


Query should look something like that, right?

<?php
$db =& JFactory::getDBO();
$query = "
  SELECT YEAR( DATE_SUB( CURDATE( ) , INTERVAL 6 YEAR ) ) ;
";
$db->setQuery($query);
$wynik = $db->loadResultArray();
?>


How to put the result into apropriate place?

I'll be greatful for any help.
GreyHead 04 Nov, 2009
Hi Tryt,

I'm a bit confused. You can only use the MySQL to select values from a database table - but there is no table involved here as far as I can see.

You certainly could do it with JavaScript -something like
var date = new Date();
var year = date.getFullYear();
. . .

wiek_1.add(Validate.Numericality, { minimum: year-6, maximum: year-2, failureMessage: 'Zła data urodzenia' });
. . .

But why not use a drop -down (or a set of radio buttons) in your form HTML to limit the choice to just those years.
<select name='year' id='year' />
<option value=''>==?==</option>
<?php
$year = date('Y');
for ( $y = $year - 6; $y <= $year -2; $y++ ) {
  echo "<option value='$y'>$y</option>";
}
?>
</select>

Bob
Tryt 04 Nov, 2009
It worked, silly me;)

Didn't thought about drop-down or radio buttons. I'll ask client.

Big thanks for help!

And about SQL - in phpMyAdmin it was working:)
This topic is locked and no more replies can be posted.