data['end_date'];$todays_date = date("Y-m-d");$today = strtotime($todays_date);$from_date = strtotime($f_date);$to_date = strtotime($t_date);if (($from_date > $today) && ($to_date > $today)) { if ($to_date validation_errors['to_date'] = "Leaving before you arrive."; }} else { $error_count++; $form->validation_errors['end_date'] = "Date in the past."; $form->validation_errors['from_date'] = "Date in the past.";} $db =& JFactory::getDBO(); $query = " SELECT `latestdate` FROM `#__chronoforms_data_HutBookingLimit` WHERE `cf_uid` = '90e7227465f69bf8ec01acc5f8c17c52' ; "; $db->setQuery($query); $d = $db->loadResult(); $F_date = strtotime( $d );if($from_date > $F_date){$error_count++;$Final_date = date( "F j, Y", strtotime( $d ) );$form->validation_errors['from_date'] = "We can not yet accept bookings after " . $Final_date. ".";}if ( $error_count ) { return false;}?>"> Date Validation Problems - Forums

Forums

Date Validation Problems

olaeblue 07 Jan, 2015
I want to have 2 date pickers with 2 'criteria'
[list=]from_date must be not before today
end_date must be after from_date[/list]

As per http://www.chronoengine.com/faqs/70-cfv5/5216-date-control-using-cfv5-date-picker-element.html Using the extra parameters box I can use The following"data-start_date=<?php echo date("d-m-Y"); ?>" Though it doesn't work if I instead use the advanced tab and put "<?php echo date("d-m-Y"); ?>" In the start date box??

But to do part 2 doesn't seem possible.

I then tried to follow the advice at https://www.chronoengine.com/faqs/64-cfv4/cfv4-using-the-datepicker/3161-how-can-i-select-a-range-of-dates-part-2.html, but the page just fails to load. Some debugging has tied this to the MooTools Custom code (page loads if I delete it but leave JS code in).

What am I doing wrong?

I have both C4 & C5 currently installed as I can't remove C4 until I can port the forms onto C5.

For info I previously did all the checking with a C4 custom server side validation using the code below, but this won't work on C5 as you have set validation 'things'. BTW the code below was cobbled and robbed from various FAQs so please don't assume much PHP knowledge when replying 😉

<?php
$error_count = 0;
$f_date = $form->data['from_date'];
$t_date = $form->data['end_date'];
$todays_date = date("Y-m-d");

$today = strtotime($todays_date);
$from_date = strtotime($f_date);
$to_date = strtotime($t_date);

if (($from_date > $today) && ($to_date > $today))  {
     if ($to_date < $from_date) {
         $error_count++;
         $form->validation_errors['to_date'] = "Leaving before you arrive.";
         }
} else {
     $error_count++;
     $form->validation_errors['end_date'] = "Date in the past.";
     $form->validation_errors['from_date'] = "Date in the past.";
}

    $db =& JFactory::getDBO();
    $query = "
      SELECT `latestdate`
        FROM `#__chronoforms_data_HutBookingLimit`
        WHERE `cf_uid` = '90e7227465f69bf8ec01acc5f8c17c52' ;
    ";
    $db->setQuery($query);
    $d = $db->loadResult();
    $F_date = strtotime( $d );

if($from_date > $F_date)
{
$error_count++;
$Final_date = date(  "F j, Y", strtotime( $d ) );
$form->validation_errors['from_date'] = "We can not yet accept bookings after " . $Final_date. ".";}

if ( $error_count ) {
  return false;
}
?>
olaeblue 07 Jan, 2015
Yes looked at all those links but they don't seem to solve my problem
Max_admin 09 Jan, 2015
Hi,

Your PHP code should not change, only $form->validation_errors[ should be changed to $form->errors[

Now regarding the first part:

if I instead use the advanced tab and put "<?php echo date("d-m-Y"); ?>" In the start date box??



Did you try this ? what happened ?

Regards,
Max
Max
ChronoForms developer...
Did you try ChronoMyAdmin for managing your Joomla database tables ?
olaeblue 09 Jan, 2015
Max

Where would I put my PHP? Previously there was a single box I put the code into, but now you have boxes for specific things like 'on empty', etc, no 'generic box'. Maybe I'm just missing something obvious.

When I put the PHP in the start date box it seemed to have no effect, I.e. I could select dates in the past. If I used the extra parameters box dates in the past were not selectable (As was my intent)
olaeblue 12 Jan, 2015
Hi Max, or any other Guru,

Can you confirm where the PHP should go in Server validation?

None of these boxes look right.

[attachment=0]Capture.JPG[/attachment]

Is there something else I should be using "Event Switcher"? It is custom validation I'm trying to do.
Max_admin 13 Jan, 2015
Hi,

For custom validation in v5 you need to use the "event switcher".

And in order to set the start date to be the date of today (dynamic date), you will need to use JavaScript code in a "Load JS" action:

jQuery(document).ready(function($) {
$("#field_id").data("start_date", '<?php echo date("d-m-Y"); ?>');
});


Regards,
Max
Max
ChronoForms developer...
Did you try ChronoMyAdmin for managing your Joomla database tables ?
olaeblue 15 Jan, 2015
Hi Max

For start date I have tried 3 ways.

1. Putting
<?php echo date("d-m-Y"); ?> 
in the Start Date field on advanced in date picker (field ID "from_date"). No effect as you can still select dates in the past.
2. Having a loadJS event before (or after) HTML Render, with the code
jQuery(document).ready(function($) {
$("from_date").data("start_date", '<?php echo date("d-m-Y"); ?>');
});
inside the JS Code field. No effect as you can still select dates in the past.
3. Having both 1 & 2. No effect as you can still select dates in the past.
4. Having
data-start_date=<?php echo date("d-m-Y"); ?>
in the extra parameters field in date picker (field ID "from_date"). This works. :o

I have no idea why the other 2 don't (and it would be nice to know why) but I have a work around. Now will go try and fix the end date issue.
olaeblue 15 Jan, 2015
So I have nearly sorted this.

For both arrival and departure date I use the following in the General Parameters field.
data-start_date =<?php echo date("d-m-Y"); ?>

or

data-end_date=<?php 
$db =& JFactory::getDBO();
    $query = "
      SELECT `latestdate`
        FROM `#__chronoengine_chronoforms_datatable_HutBookingLimit`
        WHERE `uniq_id` = '5e9dd97a2bbc25740c1e60632687716cc824c929' ;
    ";
    $db->setQuery($query);
    $d = $db->loadResult();
echo $d;
unset($d, $db, $query);?>


But I can't put both lines in the extra parameters as neither then works. Equally neither code works if I put them in the start date or end date box. So there is no way to do both. Anyone know why?

With limitations this stops dates in the past and beyond a date set on another form by admins.

It doesn't stop selecting arrival dates before departure dates.

I tried an event switcher with this final check as per the original V4 code with Max's change

<?php
$error_count = 0;
$f_date = $form->data['from_date'];
$t_date = $form->data['end_date'];
$todays_date = date("Y-m-d");

$today = strtotime($todays_date);
$from_date = strtotime($f_date);
$to_date = strtotime($t_date);

if (($from_date > $today) && ($to_date > $today))  {
     if ($to_date < $from_date) {
         $error_count++;
         $form->errors['end_date'] = "Leaving before you arrive.";
         }
} 

if ( $error_count ) {
  return false;
}
?>


And an event loop on failure. But what I get is a page like this

[attachment=0]Capture.JPG[/attachment]

I get the same thing if the Captcha is wrong, annoying as It would be better if it worked like the built in validation error pop ups. Again anyone got an idea why?

That asside finally it appears to me that a JS code in the "on date select is the way forward, but have no idea how to do this,

What would the loadJS code be to read the date (actually varChar is the datepicker output) from the field name "from_date". and check it against the value in field name "end_date"? What would I then put in the On select date box?

Any one who understands JS and could help, it would be much appreciated.

Sorry to drop multiple issues in one thread but they are all related to the same code actions so seemed better than doing 3 seperate threads.
Max_admin 15 Jan, 2015
The JS code should work, but you missed the "#" before the field id:
$("#from_date")


And in order to disable past dates for the departure field, you need to do this using JS too and set the start date of the 2nd field.

Regards,
Max
Max
ChronoForms developer...
Did you try ChronoMyAdmin for managing your Joomla database tables ?
olaeblue 15 Jan, 2015
I'm being a bit dim here so to confirm

If I put
jQuery(document).ready(function($) {
$("#from_date").data("start_date", '<?php echo date("d-m-Y"); ?>');
});


In the load js action it would set start date for from_date with nothing else in the date picker.

If I therefore put
jQuery(document).ready(function($) {
$("#from_date").data("start_date", '<?php echo date("d-m-Y"); ?>');
});
jQuery(document).ready(function($) {
$("#end_date").data("start_date", '<?php echo date("d-m-Y"); ?>');
});(


It would do it for both? Is that right?

I can then use extra parameters to limit the end date of both and all I'm left to fix is the js to prevent end before start.
olaeblue 18 Jan, 2015
Answer
Just to close this in case anyone else had similar problems

To prevent start dates in the past I used the following code in the Load JS Action (before renderHTML

jQuery(document).ready(function($) {
$("#from_date").data("start_date", '<?php echo date("d-m-Y"); ?>');
});
jQuery(document).ready(function($) {
$("#end_date").data("start_date", '<?php echo date("d-m-Y"); ?>');
});


To limit the date possible in the future (based on a field in another table) i used the following code in the Extra Parameters box of the date picker

data-end_date=<?php 
$db =& JFactory::getDBO();
    $query = "
      SELECT `latestdate`
        FROM `#__chronoengine_chronoforms_datatable_HutBookingLimit`
        WHERE `uniq_id` = '5e9dd97a2bbc25740c1e60632687716cc824c929' ;
    ";
    $db->setQuery($query);
    $d = $db->loadResult();
echo $d;
unset($d, $db, $query);?>


Finally to check the start date was before the end date I used an event switch with the following code

<?php
$error_count = 0;
$f_date = $form->data['from_date'];
$t_date = $form->data['end_date'];
$todays_date = date("Y-m-d");

$today = strtotime($todays_date);
$from_date = strtotime($f_date);
$to_date = strtotime($t_date);

if (($from_date > $today) && ($to_date > $today))  {
     if ($to_date < $from_date) {
         $error_count++;
         $form->errors['end_date'] = "Leaving before you arrive.";
         }
} 

if ( $error_count ) {
  return fail;
}
?>


Thanks Max for your help
TimC 10 Apr, 2015
Max,

Am having a slightly similar issue:

I have used the JS code example provided above to ensure the end_date for my datepicker is set to today.
    jQuery(document).ready(function($) {
        $("#startDate").data("end_date",  '<?php echo date("d-m-Y"); ?>');
    });


However, it only reflects the changed end_date after the datepicker has been opened, closed and reopened - which seems a little strange ... any ideas what I'm doing wrong ?

Incidentally, when I used the PHP code in the extra_params field of the datepicker:
<?php echo date("d-m-Y"); ?>
(as per the examples on the Chronofrms support documentation)
... it seemed to "cache" the value, such that the next day the end_date was still set to the previous day, hence I went down the JS route.

I'm sure the answer to this problem is simple - Your thoughts would be greatly appreciated.
Thanks,
Tim
GreyHead 10 Apr, 2015
Hi TimC,

Just checking - is caching enabled on your site? That might explain both behaviors.

Bob
TimC 10 Apr, 2015
Bob,

Thanks as ever for taking the time to respond.

Caching was on, however even after disabling caching the behaviour remains the same.

For clarity I have the following setup components:
- Custom Code: loads the Google Maps api and MarkerManager js files
- Joomla Plugins: as I use social media plugin across the site
- Custom Code:
- LoadJavascript: Js in which default values for the form are set and handles map based functions (this is where the end-dates are set)

- HTML Render Form

I have not tried to see what the effects are now I've disabled caching if I went back to adding the PHP code to the extra params of the datepicker and removing the JS equivalent section of code.

Incidentally, aren't there likely to be performance issues of disabling caching as I use a lot of modules?

Thanks,
Tim
TimC 10 Apr, 2015
oops - the second custom code section contains the HTML to create the social media icons
Tim
Max_admin 11 Apr, 2015
Hi,

Adding the PHP tag in the field settings will not work because the date will be created when the form is first saved.

The first solution using JS should work, I'm not sure why the datepicker has to be closed then opened for this to work, please post a link to the form or attach a demo test form for this ?

Regards,
Max
Max
ChronoForms developer...
Did you try ChronoMyAdmin for managing your Joomla database tables ?
TimC 11 Apr, 2015
Max,

Thanks for the response.

The form can be found at: https://activehouseuk.com/roso/surveillance-activity

In the "Display Options" panel at the bottom on the left the two datepickers are "Start Date" and "End Date". They both display the same behaviour and are coded according to the code I provided previously.

To recreate, simply load the page, then open either datepicker and you'll see that it is possible to select a date beyond today. Close the datepicker using the "x" then reopen and you'll see that the latest selectable date is correctly set to "today".

As an aside, I would have thought the ability to restrict date selection would be a common requirement and am surprised to learn that the PHP extra params are not used in this way, i.e. only set the once when the form is first saved. Could this be included as an update to the component, or some other method to achieve this provided in a future release ?

I have to say that Chronoforms and the date pickers are excellent and elegant and am hugely impressed by the way you and Bob respond to our users queries - thanks !

Here's hoping to get to the bottom of it.

Regards,
Tim
GreyHead 19 Apr, 2015
Hi Tim,

It's a bit tough to unravel but it looks as though you have the code snippet including the jQuery(document).ready() inside a setDefaultValues() function being called by the Google Maps Initialize() function which is in turn called by this line
google.maps.event.addDomListener(window,'load', Initialize);

I suspect that there is a sequencing problem here and that when that first runs the date-pickers may not be in place???

Try pulling this out of the Google code as a stand alone snippet in the Load JavaScript action.

Bob
TimC 29 Apr, 2015
Bob,

My apologies for the delay in getting around to implementing this change and responding to you - especially after the effort you've put in (and Max) to help me resolve this.

I am pleased to say that your suggestion worked !! Thanks a million.

It was causing me a lot of pain as Javascript is not my first language ! When I read your response it seemed perfectly logical and is exactly the correct fix.

Thanks again,
Tim
Thuridt 15 Oct, 2015
Hello,

I've got the same problems with the validation of my datepickers. I tried your ideas and the first one works perfect. But I've some problems with the event switcher. Your solutions doesn't work for me.


Finally to check the start date was before the end date I used an event switch with the following code

<?php
$error_count = 0;
$f_date = $form->data['from_date'];
$t_date = $form->data['end_date'];
$todays_date = date("Y-m-d");

$today = strtotime($todays_date);
$from_date = strtotime($f_date);
$to_date = strtotime($t_date);

if (($from_date > $today) && ($to_date > $today))  {
     if ($to_date < $from_date) {
         $error_count++;
         $form->errors['end_date'] = "Leaving before you arrive.";
         }
} 

if ( $error_count ) {
  return fail;
}
?>



I changed the names of the fields "from_date" and "end_date" to my field IDs. I don't really know where to put the event switcher. I tried in the onload event and in the submit event but nothing works.

I hope I get some help🙂

Regards,
Thuridt
GreyHead 16 Oct, 2015
Hi Thuridt,

This is a long thread with a lot of posts in it. The code you have picked up was for the Event Switcher in CFv4 and you are using CFv5.

If you are checking the dates after the form submits then the Event Switcher needs to be in the On Submit event.

The problem I see is that you need quotes round 'fail' at the end
if ( $error_count ) {
  return 'fail';
}
?>

Bob
Thuridt 16 Oct, 2015
Hi Bob,

thank you for your reply. I tried your code but it didn't work. The JPEG shows my administration area in CF.

Here is this link to my testing area: http://testdrive.frontpix.com/anfragen-esel I added a debugger but there is shown no failure.

I've no idea to fix it😟

Regards,
Thuridt
GreyHead 16 Oct, 2015
Hi Thuridt,

Please try this version
<?php
$error_count = 0;
$f_date = $form->data['from_date'];
$e_date = $form->data['end_date'];
$todays_date = date("Y-m-d");

$today = strtotime($todays_date);
$from_date = strtotime($f_date);
$end_date = strtotime($e_date);

if ( ($from_date <= $today) || ($end_date <= $today) ) {
  $error_count++;
  $form->errors['from_date'] = "One of those dates is in the past.";
}
if ( $end_date < $from_date ) {
  $error_count++;
  $form->errors['end_date'] = "Leaving before you arrive.";
}

if ( $error_count ) {
  return 'fail';
}
?>

Bob
Thuridt 16 Oct, 2015
Hi Bob,

the first part of your code (from_date <= today) works fine but in my datepicker there is no way to choose a date in the past (before today). The second part end_date < from_date doesn't work.

I don't know why.

Regards,
Thuridt
Thuridt 16 Oct, 2015
Hi Bob,

it works😀 I changed your code a little bit
<?php
    $error_count = 0;
    $f_date = $form->data['datepicker7'];
    $e_date = $form->data['datepicker8'];
    $todays_date = date("d-m-Y");

    $today = strtotime($todays_date);
    $from_date = strtotime($f_date);
    $end_date = strtotime($e_date);

    if ( $end_date <= $from_date ) {
      $error_count++;
      $form->errors['datepicker8'] = "Abreisedatum liegt vor Anreisedatum.";
    }

    if ( $error_count ) {
      return 'fail';
    }
    ?>


But now I've another problem. In my E-Mail input field there is the following message after producing a failure. How can I fix this ?

Regards,
Thuridt
Thuridt 17 Oct, 2015
Hi Bob,

thank you very much. Everything works perfect😀

Best regards,
Thuridt
This topic is locked and no more replies can be posted.