Questions: add min-length to string/text type questions (#6488)

* Questions: add min-length to string/text type questions

* fix test
This commit is contained in:
Richard Schreiber
2026-08-25 09:37:37 +02:00
committed by GitHub
parent 4755e7af0e
commit 6d4aba6e3d
9 changed files with 47 additions and 4 deletions
+1 -1
View File
@@ -550,7 +550,7 @@ class QuestionSerializer(I18nAwareModelSerializer):
'ask_during_checkin', 'show_during_checkin', 'identifier', 'dependency_question', 'dependency_values',
'hidden', 'dependency_value', 'print_on_invoice', 'help_text', 'valid_number_min',
'valid_number_max', 'valid_date_min', 'valid_date_max', 'valid_datetime_min', 'valid_datetime_max',
'valid_string_length_max', 'valid_file_portrait')
'valid_string_length_max', 'valid_string_length_min', 'valid_file_portrait')
def validate_identifier(self, value):
Question._clean_identifier(self.context['event'], value, self.instance)
+2
View File
@@ -708,6 +708,7 @@ class BaseQuestionsForm(forms.Form):
elif q.type == Question.TYPE_STRING:
field = forms.CharField(
label=escape(q.question), required=required,
min_length=q.valid_string_length_min,
max_length=q.valid_string_length_max,
help_text=rich_text(q.help_text),
initial=initial.answer if initial else None,
@@ -715,6 +716,7 @@ class BaseQuestionsForm(forms.Form):
elif q.type == Question.TYPE_TEXT:
field = forms.CharField(
label=escape(q.question), required=required,
min_length=q.valid_string_length_min,
max_length=q.valid_string_length_max,
help_text=rich_text(q.help_text),
widget=forms.Textarea,
@@ -0,0 +1,19 @@
# Generated by Django 5.2.12 on 2026-08-19 11:31
import django.db.models.deletion
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("pretixbase", "0309_alter_questionanswer_unique_together_and_more"),
]
operations = [
migrations.AddField(
model_name="question",
name="valid_string_length_min",
field=models.PositiveIntegerField(null=True),
),
]
+13 -1
View File
@@ -50,7 +50,7 @@ from dateutil.tz import datetime_exists
from django.conf import settings
from django.core.exceptions import ValidationError
from django.core.validators import (
MaxLengthValidator, MinValueValidator, RegexValidator,
MaxLengthValidator, MinLengthValidator, MinValueValidator, RegexValidator,
)
from django.db import models
from django.db.models import Q
@@ -1731,6 +1731,11 @@ class Question(LoggedModel):
valid_datetime_max = models.DateTimeField(null=True, blank=True,
verbose_name=_('Maximum value'),
help_text=_('Currently not supported in our apps and during check-in'))
valid_string_length_min = models.PositiveIntegerField(null=True, blank=True,
verbose_name=_('Minimum length'),
help_text=_(
'Currently not supported in our apps and during check-in'
))
valid_string_length_max = models.PositiveIntegerField(null=True, blank=True,
verbose_name=_('Maximum length'),
help_text=_(
@@ -1885,6 +1890,11 @@ class Question(LoggedModel):
else:
raise ValidationError(_('Unknown country code.'))
elif self.type in (Question.TYPE_STRING, Question.TYPE_TEXT):
if self.valid_string_length_min is not None and len(answer) < self.valid_string_length_min:
raise ValidationError(MinLengthValidator.message % {
'limit_value': self.valid_string_length_min,
'show_value': len(answer)
})
if self.valid_string_length_max is not None and len(answer) > self.valid_string_length_max:
raise ValidationError(MaxLengthValidator.message % {
'limit_value': self.valid_string_length_max,
@@ -1906,6 +1916,8 @@ class Question(LoggedModel):
raise ValidationError(_("The maximum date must not be before the minimum value."))
if self.valid_number_max and self.valid_number_min and self.valid_number_min > self.valid_number_max:
raise ValidationError(_("The maximum value must not be lower than the minimum value."))
if self.valid_string_length_max and self.valid_string_length_min and self.valid_string_length_min > self.valid_string_length_max:
raise ValidationError(_("The maximum length must not be shorter than the minimum length."))
super().clean()
def clean_type_change(self, old_type, new_type):
+1
View File
@@ -251,6 +251,7 @@ class QuestionForm(I18nModelForm):
'valid_date_min',
'valid_date_max',
'valid_file_portrait',
'valid_string_length_min',
'valid_string_length_max',
]
widgets = {
@@ -47,6 +47,7 @@
{% bootstrap_field form.valid_datetime_max layout="control" %}
</div>
<div id="valid-string">
{% bootstrap_field form.valid_string_length_min layout="control" %}
{% bootstrap_field form.valid_string_length_max layout="control" %}
</div>
<div id="valid-file">