Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow settings a 'future date' restriction on date fields #26

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions assets/js/backend/wizard-admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ declare var wp: any;
dateHtml += '<label>' + wizard.i18n.dateformat + '<a target="_blank" href="http://t1m0n.name/air-datepicker/docs/#sub-section-9"><i class="fa fa-info-circle" aria-hidden="true" title="' + wizard.i18n.tooltips.dateformat + '"></i></a></label>';
dateHtml += '<input type="text" class="fw-date-format fw-block-label" placeholder="' + wizard.i18n.dateformat + '" value="' + escapeHtml(block.format ? block.format : 'yy-mm-dd') + '" ></input><br/>';
dateHtml += '<label><input type="checkbox" class="fw-required"' + isChecked(block.required) + '/>' + wizard.i18n.required + '</label>';
dateHtml += '<label><input type="checkbox" class="fw-allowfuturedates"' + isChecked(block.allowfuturedates) + '/>' + wizard.i18n.allowfuturedates + '</label>';
return dateHtml;
}

Expand Down Expand Up @@ -662,6 +663,7 @@ declare var wp: any;
text['label'] = $text.find('.fw-text-label').val();
text['format'] = $text.find('.fw-date-format').val();
text['required'] = $text.find('.fw-required').prop('checked');
text['allowfuturedates'] = $text.find('.fw-allowfuturedates').prop('checked');
}

function getTextareaData($text, text) {
Expand Down
39 changes: 37 additions & 2 deletions assets/js/frontend/frontend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -600,13 +600,13 @@ jQuery(document).ready(function ($) {
return false;
}
if (minimum !== undefined && parseInt(minimum, 10) !== NaN) {
if (parseInt(minimum, 10) > numericValue) {
if (parseInt(minimum, 10) > numeric.length) {
$element.addClass('fw-block-invalid');
return false;
}
}
if (maximum !== undefined && parseInt(maximum, 10) !== NaN) {
if (parseInt(maximum, 10) < numericValue) {
if (parseInt(maximum, 10) < numeric.length) {
$element.addClass('fw-block-invalid');
return false;
}
Expand All @@ -629,6 +629,41 @@ jQuery(document).ready(function ($) {
$element.addClass('fw-block-invalid');
return false;
} else {
const allowFutureDates = $element[0].getAttribute('data-allowfuturedates') === 'true'
if (!allowFutureDates) {
const input = $element[0].querySelector('.fw-datepicker-here')
const dateFormat = input.getAttribute('data-dateformat')
let day, month, year
switch (dateFormat) {
case 'yy-mm-dd':
[year, month, day] = input.value.split('-')
break
case 'dd-mm-yy':
[day, month, year] = input.value.split('-')
break
case 'yy/mm/dd':
[year, month, day] = input.value.split('/')
break
case 'dd/mm/yy':
[day, month, year] = input.value.split('/')
break
default:
throw new Error('Future dates are not allowed, but the chosen date could not be parsed')
}
day = Number(day) < 10 ? `0${Number(day)}` : Number(day)
month = Number(month) < 10 ? `0${Number(month)}` : Number(month)
const chosenDate = Date.parse(`${year}-${month}-${day}T00:00:00.000`)
const today = new Date()
const thisMonth = today.getMonth() < 9 ? `0${today.getMonth() + 1}` : today.getMonth() + 1
const thisDate = today.getDate() < 10 ? `0${today.getDate()}` : today.getDate()
const formatToday = Date.parse(
`${today.getFullYear()}-${thisMonth}-${thisDate}T00:00:00.000`
)
if (chosenDate >= (formatToday + 86400000)) {
alert(msfAjax.i18n.errors.noFutureDates)
}
return chosenDate < (formatToday + 86400000)
}
return true;
}
}
Expand Down
1 change: 1 addition & 0 deletions includes/admin/msf-admin.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public static function get_translation() {
'label' => __('Label', 'multi-step-form'),
'multifile' => __('Multiple Files', 'multi-step-form'),
'dateformat' => __('Date Format', 'multi-step-form'),
'allowfuturedates' => __('Allow future dates', 'multi-step-form'),
'required' => __('Required', 'multi-step-form'),
'radio' => array(
'header' => __('Header', 'multi-step-form'),
Expand Down
12 changes: 8 additions & 4 deletions includes/lib/msf-blocks/date/msf-block-date.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Mondula_Form_Wizard_Block_Date extends Mondula_Form_Wizard_Block {
private $_label;
private $_required;
private $_format;
private $_allowfuturedates;

protected static $type = "fw-date";

Expand All @@ -21,20 +22,21 @@ class Mondula_Form_Wizard_Block_Date extends Mondula_Form_Wizard_Block {
* @param boolean $required The If true, Input for this field is required.
* @param string $format The Format the Date will be shown in.
*/
public function __construct ($label, $required, $format) {
public function __construct ($label, $required, $format, $allowfuturedates = true) {
$this->_label = $label;
$this->_required = $required;
if (!empty($format)) {
$this->_format = $format;
} else {
$this->_format = 'yy-mm-dd';
}
$this->_allowfuturedates = $allowfuturedates;
}

public function render($ids) {
$locale = substr(get_locale(), 0, 2) === 'de' ? 'de' : 'en'; // TODO this is possibly not a good idea
?>
<div class="fw-step-block" data-blockId="<?php echo $ids[0]; ?>" data-type="fw-date" data-required="<?php echo $this->_required; ?>">
<div class="fw-step-block" data-blockId="<?php echo $ids[0]; ?>" data-type="fw-date" data-required="<?php echo $this->_required; ?>" data-allowfuturedates="<?php echo $this->_allowfuturedates; ?>">
<div class="fw-input-container">
<label for="msf-date-<?php echo str_replace(' ', '-', strtolower($this->_label)); ?>"><h3><?php echo $this->_label ?></h3></label>
<input type="text" id="msf-date-<?php echo str_replace(' ', '-', strtolower($this->_label)); ?>" class="fw-text-input fw-datepicker-here" data-id="date" data-language="<?php echo $locale; ?>" data-dateformat="<?php echo esc_attr($this->_format) ?>">
Expand All @@ -50,15 +52,17 @@ public function as_aa() {
'type' => 'date',
'label' => $this->_label,
'required' => $this->_required,
'format' => $this->_format
'format' => $this->_format,
'allowfuturedates' => $this->_allowfuturedates
);
}

public static function from_aa($aa , $current_version, $serialized_version) {
$label = $aa['label'];
$required = $aa['required'];
$format = $aa['format'];
return new Mondula_Form_Wizard_Block_Date($label, $required, $format);
$allowfuturedates = $aa['allowfuturedates'];
return new Mondula_Form_Wizard_Block_Date($label, $required, $format, $allowfuturedates);
}

public static function addType($types) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public static function from_aa($aa , $current_version, $serialized_version) {
}

if (strlen($minimum) > 0 && strlen($maximum) > 0) {
if (intval($minimum) >= intval($maximum)) {
if (intval($minimum) > intval($maximum)) {
$maximum = strval(intval($minimum) + 1);
}
}
Expand Down
1 change: 1 addition & 0 deletions includes/msf.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ public static function get_translation() {
'invalidRegex' => __('Invalid input', 'multi-step-form'),
'noCaptcha' => __('Please solve the captcha first!', 'multi-step-form'),
'emailsDontMatch' => __('Emails provided don\'t match!', 'multi-step-form'),
'noFutureDates' => __('No future dates allowed', 'multi-step-form'),
),
);
}
Expand Down
88 changes: 48 additions & 40 deletions lang/multi-step-form.pot
Original file line number Diff line number Diff line change
Expand Up @@ -150,162 +150,166 @@ msgid "Date Format"
msgstr ""

#: includes/admin/msf-admin.class.php:89
msgid "Allow future dates"
msgstr ""

#: includes/admin/msf-admin.class.php:90
msgid "Required"
msgstr ""

#: includes/admin/msf-admin.class.php:91
#: includes/admin/msf-admin.class.php:92
msgid "Header"
msgstr ""

#: includes/admin/msf-admin.class.php:92
#: includes/admin/msf-admin.class.php:93
msgid "Option"
msgstr ""

#: includes/admin/msf-admin.class.php:93
#: includes/admin/msf-admin.class.php:94
msgid "Options"
msgstr ""

#: includes/admin/msf-admin.class.php:94
#: includes/admin/msf-admin.class.php:95
msgid "Add option"
msgstr ""

#: includes/admin/msf-admin.class.php:95
#: includes/admin/msf-admin.class.php:96
msgid "Multiple Selection"
msgstr ""

#: includes/admin/msf-admin.class.php:98
#: includes/admin/msf-admin.class.php:99
msgid "Options (one per line)"
msgstr ""

#: includes/admin/msf-admin.class.php:99
#: includes/admin/msf-admin.class.php:100
msgid "Enable search"
msgstr ""

#: includes/admin/msf-admin.class.php:100
#: includes/admin/msf-admin.class.php:101
msgid "Set placeholder"
msgstr ""

#: includes/admin/msf-admin.class.php:103
#: includes/admin/msf-admin.class.php:104
msgid "Text"
msgstr ""

#: includes/admin/msf-admin.class.php:104
#: includes/admin/msf-admin.class.php:105
msgid "Paragraph text"
msgstr ""

#: includes/admin/msf-admin.class.php:107, includes/lib/msf-blocks/media/msf-block-media.class.php:59
#: includes/admin/msf-admin.class.php:108, includes/lib/msf-blocks/media/msf-block-media.class.php:59
msgid "Media"
msgstr ""

#: includes/admin/msf-admin.class.php:108
#: includes/admin/msf-admin.class.php:109
msgid "Select Media"
msgstr ""

#: includes/admin/msf-admin.class.php:109
#: includes/admin/msf-admin.class.php:110
msgid "Place an image or video in your form."
msgstr ""

#: includes/admin/msf-admin.class.php:110
#: includes/admin/msf-admin.class.php:111
msgid "Select a media element"
msgstr ""

#: includes/admin/msf-admin.class.php:111
#: includes/admin/msf-admin.class.php:112
msgid "Title"
msgstr ""

#: includes/admin/msf-admin.class.php:112
#: includes/admin/msf-admin.class.php:113
msgid "Filename"
msgstr ""

#: includes/admin/msf-admin.class.php:113
#: includes/admin/msf-admin.class.php:114
msgid "Preview"
msgstr ""

#: includes/admin/msf-admin.class.php:116
#: includes/admin/msf-admin.class.php:117
msgid "GET Parameter"
msgstr ""

#: includes/admin/msf-admin.class.php:119
#: includes/admin/msf-admin.class.php:120
msgid "Minimum"
msgstr ""

#: includes/admin/msf-admin.class.php:120
#: includes/admin/msf-admin.class.php:121
msgid "Maximum"
msgstr ""

#: includes/admin/msf-admin.class.php:121
#: includes/admin/msf-admin.class.php:122
msgid "No Minimum"
msgstr ""

#: includes/admin/msf-admin.class.php:122
#: includes/admin/msf-admin.class.php:123
msgid "No Maximum"
msgstr ""

#: includes/admin/msf-admin.class.php:125
#: includes/admin/msf-admin.class.php:126
msgid "Confirm"
msgstr ""

#: includes/admin/msf-admin.class.php:127
#: includes/admin/msf-admin.class.php:128
msgid "RegEx Filter"
msgstr ""

#: includes/admin/msf-admin.class.php:128
#: includes/admin/msf-admin.class.php:129
msgid "Custom RegEx Error Message"
msgstr ""

#: includes/admin/msf-admin.class.php:130
#: includes/admin/msf-admin.class.php:131
msgid "Please select the registration fields to be displayed to the user. Email is always required. If the user does not specify a username or password, WordPress is auto-generating these and sending them to the user via email."
msgstr ""

#: includes/admin/msf-admin.class.php:131
#: includes/admin/msf-admin.class.php:132
msgid "You are already registered and logged in."
msgstr ""

#: includes/admin/msf-admin.class.php:132
#: includes/admin/msf-admin.class.php:133
msgid "Username"
msgstr ""

#: includes/admin/msf-admin.class.php:133, includes/lib/msf-blocks/email/msf-block-email.class.php:69, includes/msf-settings.class.php:48
#: includes/admin/msf-admin.class.php:134, includes/lib/msf-blocks/email/msf-block-email.class.php:69, includes/msf-settings.class.php:48
msgid "Email"
msgstr ""

#: includes/admin/msf-admin.class.php:134
#: includes/admin/msf-admin.class.php:135
msgid "Password"
msgstr ""

#: includes/admin/msf-admin.class.php:135
#: includes/admin/msf-admin.class.php:136
msgid "Confirm Password"
msgstr ""

#: includes/admin/msf-admin.class.php:136
#: includes/admin/msf-admin.class.php:137
msgid "First Name"
msgstr ""

#: includes/admin/msf-admin.class.php:137
#: includes/admin/msf-admin.class.php:138
msgid "Last Name"
msgstr ""

#: includes/admin/msf-admin.class.php:138
#: includes/admin/msf-admin.class.php:139
msgid "Website"
msgstr ""

#: includes/admin/msf-admin.class.php:139
#: includes/admin/msf-admin.class.php:140
msgid "Biographical Info"
msgstr ""

#: includes/admin/msf-admin.class.php:188
#: includes/admin/msf-admin.class.php:189
msgid "Not installed/active"
msgstr ""

#: includes/admin/msf-admin.class.php:287
#: includes/admin/msf-admin.class.php:288
msgid "Invalid JSON-File. Check your syntax."
msgstr ""

#: includes/admin/msf-admin.class.php:315
#: includes/admin/msf-admin.class.php:316
msgid "Forms must be imported as JSON files"
msgstr ""

#: includes/admin/msf-admin.class.php:420, includes/admin/partials/msf-editor.php:8
#: includes/admin/msf-admin.class.php:421, includes/admin/partials/msf-editor.php:8
msgid "Success. Form saved."
msgstr ""

Expand Down Expand Up @@ -561,7 +565,7 @@ msgstr ""
msgid "Please send us a screenshot of the following information, if you write us because of a technical problem."
msgstr ""

#: includes/lib/msf-blocks/date/msf-block-date.class.php:68
#: includes/lib/msf-blocks/date/msf-block-date.class.php:72
msgid "Date"
msgstr ""

Expand Down Expand Up @@ -924,3 +928,7 @@ msgstr ""
#: includes/msf.class.php:172
msgid "Emails provided don't match!"
msgstr ""

#: includes/msf.class.php:173
msgid "No future dates allowed"
msgstr ""