Home › Forums › Quform WordPress › Date validation logic not working
- This topic has 1 reply, 2 voices, and was last updated 10 years, 11 months ago by
Ally.
- AuthorPosts
- September 18, 2014 at 5:33 pm #12494
Nhus
ParticipantHello, I’m building a check-in/check-out booking system based on Quforms, I followed the steps on https://support.themecatcher.net/quform-wordpress/guides/advanced/date-validation for making a booking system but I have encountered a few problems:
1- Preventing past dates works fine but when I move forward with the next function on the tutorial I can select any date and the default date on the time-picker is the current day and the current year but in January.
2- Preventing specific dates from being chosen is not working.
This is how my child theme function.php looks like:
// Prevent past dates from being chosen
function mytheme_prevent_past_dates($valid, $value, $element)
{
$time = strtotime("{$value['year']}-{$value['month']}-{$value['day']}");if ($time < strtotime('today')) {
$element->addError('Please choose a date in the future');
$valid = false;
}return $valid;
}
add_filter('iphorm_element_valid_iphorm_1_1', 'mytheme_prevent_past_dates', 10, 3);function mytheme_datepicker_prevent_past_dates($options, $dpMinYear, $dpMaxYear, $element)
{
$year = date('Y'); // Today's year
$month = date('n'); // Today's month
$day = date('j'); // Today's dayreturn "{
minDate: new Date({$year}, {$month} - 1, {$day}),
maxDate: new Date({$dpMaxYear}, 12 - 1, 31)
}";
}
add_filter('iphorm_datepicker_options_iphorm_1_1', 'mytheme_datepicker_prevent_past_dates', 10, 4);//Prevent booking dates from being chosen
function mytheme_get_dates_to_disable()
{
global $wpdb;$dates = $wpdb->get_results('SELECT
date
FROM wp_bookings;', ARRAY_A);
$disabledDates = array();if (is_array($dates)) {
foreach ($dates as $date) {
$disabledDates[] = $date['date'];
}
}return $disabledDates;
}//Prevent booking dates from being chosen
function mytheme_prevent_specific_dates($valid, $value, $element)
{
if ($valid) {
$disabledDates = mytheme_get_dates_to_disable();
$submittedDate = "{$value['year']}-{$value['month']}-{$value['day']}";if (in_array($submittedDate, $disabledDates)) {
$element->addError('That date is not available');
$valid = false;
}
}return $valid;
}
add_filter('iphorm_element_valid_iphorm_1_1', 'mytheme_prevent_specific_dates', 10, 3);function mytheme_datepicker_prevent_specific_dates($options, $dpMinYear, $dpMaxYear, $element)
{
return "{
beforeShowDay: function (date) {
if (quformDisabledDates) {
for (var i = 0; i < quformDisabledDates.length; i++) {
var parts = quformDisabledDates[i].split('-');
if (date.getFullYear() == parts[0] && (date.getMonth()+1) == parts[1] && date.getDate() == parts[2]) {
return [false];
}
}
}
return [true];
}
}";
}
add_filter('iphorm_datepicker_options_iphorm_1_1', 'mytheme_datepicker_prevent_specific_dates', 10, 4);function mytheme_print_dates_to_disable()
{
?>
<script type="text/javascript">
var quformDisabledDates = <?php echo json_encode(mytheme_get_dates_to_disable()); ?>;
</script>
<?php
}
add_action('wp_head', 'mytheme_print_dates_to_disable');//Validating booking dates range
function mytheme_check_date_range($valid, $value, $element)
{
// Get the value of the start date element
$startDate = $element->getForm()->getValue('iphorm_1_1');$startDate = strtotime("{$startDate['year']}-{$startDate['month']}-{$startDate['day']} 00:00:00");
$endDate = strtotime("{$value['year']}-{$value['month']}-{$value['day']} 23:59:59");if ($startDate > $endDate) {
$element->addError('The end date must be after the start date');
$valid = false;
}return $valid;
}add_filter('iphorm_element_valid_iphorm_1_2', 'mytheme_check_date_range', 10, 3);
//Save Booking dates
function mytheme_save_booking_date($form)
{
global $wpdb;$date = $form->getValue('iphorm_1_1');
$wpdb->insert('wp_bookings', array(
'date' => $date['year'] . '-' . $date['month'] . '-' . $date['day']
));$date = $form->getValue('iphorm_1_2');
$wpdb->insert('wp_bookings', array(
'date' => $date['year'] . '-' . $date['month'] . '-' . $date['day']
));
}
add_action('iphorm_post_process_1', 'mytheme_save_booking_date', 10, 1);My form ID is 1, my check-in date element ID is iphorm_1_1, the check-out date element ID is iphorm_1_2
I look forward to your feedback.
September 22, 2014 at 5:06 pm #12558Ally
Support StaffYou don't have permission to view this content. Please log in or register and then verify your purchases to gain access.
- AuthorPosts
- You must be logged in to reply to this topic.