Fix date field display with readonly TextInput widget
- Changed from disabled DateInput to readonly TextInput for auto-generated payments - Uses German date format (dd.mm.yyyy) for better user experience - Added visual styling to indicate readonly state - Preserves original date value through clean() method
This commit is contained in:
@@ -998,18 +998,27 @@ class DestinataerUnterstuetzungForm(forms.ModelForm):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
# Make faellig_am disabled for automatically generated quarterly payments
|
||||
# Make faellig_am read-only for automatically generated quarterly payments
|
||||
self.is_auto_generated = False
|
||||
if self.instance and self.instance.pk and self.instance.beschreibung:
|
||||
if "Vierteljährliche Unterstützung" in self.instance.beschreibung and "(automatisch erstellt)" in self.instance.beschreibung:
|
||||
self.is_auto_generated = True
|
||||
self.fields['faellig_am'].disabled = True
|
||||
|
||||
# Use a TextInput widget with readonly attribute to display the date
|
||||
from django import forms
|
||||
current_date = self.instance.faellig_am
|
||||
if current_date:
|
||||
self.fields['faellig_am'].widget = forms.TextInput(
|
||||
attrs={
|
||||
"class": "form-control",
|
||||
"readonly": True,
|
||||
"value": current_date.strftime('%d.%m.%Y'), # German date format
|
||||
"style": "background-color: #f8f9fa; cursor: not-allowed;"
|
||||
}
|
||||
)
|
||||
self.fields['faellig_am'].initial = current_date
|
||||
|
||||
self.fields['faellig_am'].help_text = "Fälligkeitsdatum wird automatisch basierend auf Quartal berechnet"
|
||||
# Ensure the initial value is set for disabled field and widget shows the value
|
||||
self.fields['faellig_am'].initial = self.instance.faellig_am
|
||||
self.fields['faellig_am'].widget.attrs.update({
|
||||
'value': self.instance.faellig_am.strftime('%Y-%m-%d') if self.instance.faellig_am else ''
|
||||
})
|
||||
|
||||
def clean(self):
|
||||
cleaned_data = super().clean()
|
||||
|
||||
Reference in New Issue
Block a user