Do django-select2 form fields default to required=False?
engin-can opened this issue · comments
Hi, I noticed my select2 fields don't have the required
attribute in HTML? According to Django's form documentation the default required
value is True
. Any idea if select2 is defaulting to False
?
class CustomFroalaEditor(FroalaEditor):
def __init__(self, *args, **kwargs):
super(CustomFroalaEditor, self).__init__(*args, **kwargs)
def render(self, name, value, attrs=None, renderer=None):
html = super(FroalaEditor, self).render(name, value, attrs)
return mark_safe(html)
class NewBlaForm(forms.ModelForm):
company = forms.ChoiceField(widget=forms.Select(attrs={'id': 'nbc__name-dropdown', 'class': 'form-control'}))
product = forms.ChoiceField(widget=forms.Select(attrs={'id': 'nbp__name-dropdown', 'class': 'form-control', 'disabled': 'disabled'}))
class Meta:
model = Bla
fields = ('product')
Hi @fbengo thanks for reaching out. That's a good questions. This package does not alter the default behavior in Django. Therefore, by default all fields are required.
Select2 works a bit different from your regular select
-tag in HTML. We actually set a special data
-attribute to make the selection required. We use whatever was passed by the form or whenever else the widget was initialized. See also:
django-select2/django_select2/forms.py
Lines 76 to 80 in 2bb6f3a