add js to prevent illegal inputs

This commit is contained in:
Lukas Bockstaller
2026-05-07 10:53:52 +02:00
parent 8adc649c4c
commit 4db98def79
4 changed files with 50 additions and 1 deletions

View File

@@ -21,7 +21,7 @@
# #
import datetime import datetime
from collections import namedtuple from collections import namedtuple
from typing import Union from typing import Tuple, Union
from zoneinfo import ZoneInfo from zoneinfo import ZoneInfo
from dateutil import parser from dateutil import parser
@@ -42,6 +42,7 @@ EVENT_CHOICES = (
('presale_end', _('Presale end')), ('presale_end', _('Presale end')),
) )
# extend NO_BEFORE_VALUES in reldate.js if changed
ORDER_CHOICES = ( ORDER_CHOICES = (
('datetime', _('Moment of order')), ('datetime', _('Moment of order')),
) )

View File

@@ -1,4 +1,6 @@
{% load i18n %} {% load i18n %}
{% load static %}
<script src="{% static 'pretixbase/js/reldate.js' %}" defer></script>
<div class="reldatetime"> <div class="reldatetime">
{% for group_name, group_choices, group-index in widget.subwidgets.0.optgroups %} {% for group_name, group_choices, group-index in widget.subwidgets.0.optgroups %}
{% for selopt in group_choices %} {% for selopt in group_choices %}

View File

@@ -1,4 +1,6 @@
{% load i18n %} {% load i18n %}
{% load static %}
<script src="{% static 'pretixbase/js/reldate.js' %}" defer></script>
<div class="reldatetime"> <div class="reldatetime">
{% for group_name, group_choices, group-index in widget.subwidgets.0.optgroups %} {% for group_name, group_choices, group-index in widget.subwidgets.0.optgroups %}
{% for selopt in group_choices %} {% for selopt in group_choices %}

View File

@@ -0,0 +1,44 @@
if (!window.__reldateInitialized) {
window.__reldateInitialized = true;
document.addEventListener('DOMContentLoaded', () => {
const NO_BEFORE_VALUES = ['datetime'];
document.querySelectorAll('.reldatetime, .reldate').forEach(container => {
const groups = container.querySelectorAll('.radio');
groups.forEach(group => {
const selects = group.querySelectorAll('select');
if (selects.length < 2) return;
let referenceSelect = null;
let beforeAfterSelect = null;
selects.forEach(sel => {
const values = Array.from(sel.options).map(o => o.value);
// only attach to selects that contain problematic values
if (NO_BEFORE_VALUES.some(v => values.includes(v))) {
referenceSelect = sel;
} else if (values.includes('before') && values.includes('after')) {
beforeAfterSelect = sel;
}
});
if (!referenceSelect || !beforeAfterSelect) return;
const beforeOption = beforeAfterSelect.querySelector('option[value="before"]');
const updateBeforeOption = () => {
if (NO_BEFORE_VALUES.includes(referenceSelect.value)) {
beforeOption.disabled = true;
if (beforeAfterSelect.value === 'before') {
beforeAfterSelect.value = 'after';
beforeAfterSelect.dispatchEvent(new Event('change', { bubbles: true }));
}
} else {
beforeOption.disabled = false;
}
};
referenceSelect.addEventListener('change', updateBeforeOption);
updateBeforeOption();
});
});
});
}