Form utils for bulma
This commit is contained in:
parent
2e28986503
commit
6e88f1a887
10 changed files with 268 additions and 0 deletions
16
bds/templates/bds/forms/checkbox.html
Normal file
16
bds/templates/bds/forms/checkbox.html
Normal file
|
@ -0,0 +1,16 @@
|
|||
<div class="control">
|
||||
{% if field.auto_id %}
|
||||
<label class="checkbox {% if field.field.required %}{{ form.required_css_class }}{% endif %}">
|
||||
{{ field }} {{ field.label }}
|
||||
</label>
|
||||
{% endif %}
|
||||
{% for error in field.errors %}
|
||||
<span class="help is-danger {{ form.error_css_class }}">{{ error }}</span>
|
||||
{% endfor %}
|
||||
|
||||
{% if field.help_text %}
|
||||
<p class="help">
|
||||
{{ field.help_text|safe }}
|
||||
</p>
|
||||
{% endif %}
|
||||
</div>
|
33
bds/templates/bds/forms/field.html
Normal file
33
bds/templates/bds/forms/field.html
Normal file
|
@ -0,0 +1,33 @@
|
|||
{% load bulma_utils %}
|
||||
|
||||
<div class="field">
|
||||
{% if field|is_checkbox %}
|
||||
|
||||
{% include "bds/forms/checkbox.html" with field=field %}
|
||||
|
||||
{% elif field|is_radio %}
|
||||
|
||||
{% include "bds/forms/radio.html" with field=field %}
|
||||
|
||||
{% elif field|is_input %}
|
||||
|
||||
{% include "bds/forms/input.html" with field=field %}
|
||||
|
||||
{% elif field|is_textarea %}
|
||||
|
||||
{% include "bds/forms/textarea.html" with field=field %}
|
||||
|
||||
{% elif field|is_select %}
|
||||
|
||||
{% include "bds/forms/select.html" with field=field %}
|
||||
|
||||
{% elif field|is_file %}
|
||||
|
||||
{% include "bds/forms/file.html" with field=field %}
|
||||
|
||||
{% else %}
|
||||
|
||||
{% include "bds/forms/other.html" with field=field %}
|
||||
|
||||
{% endif %}
|
||||
</div>
|
31
bds/templates/bds/forms/file.html
Normal file
31
bds/templates/bds/forms/file.html
Normal file
|
@ -0,0 +1,31 @@
|
|||
{% load bulma_utils %}
|
||||
{% load i18n %}
|
||||
|
||||
<label class="label {% if field.field.required %}{{ form.required_css_class }}{% endif %}">
|
||||
{{ field.label }}
|
||||
</label>
|
||||
|
||||
<div class="control">
|
||||
|
||||
<label class="file-label">
|
||||
{{ field|addclass:'file-input' }}
|
||||
<span class="file-cta">
|
||||
<span class="file-icon">
|
||||
<i class="fa fa-upload"></i>
|
||||
</span>
|
||||
<span class="file-label">
|
||||
{% trans "Choisissez un fichier..." %}
|
||||
</span>
|
||||
</span>
|
||||
</label>
|
||||
|
||||
{% for error in field.errors %}
|
||||
<span class="help is-danger {{ form.error_css_class }}">{{ error }}</span>
|
||||
{% endfor %}
|
||||
|
||||
{% if field.help_text %}
|
||||
<p class="help">
|
||||
{{ field.help_text|safe }}
|
||||
</p>
|
||||
{% endif %}
|
||||
</div>
|
22
bds/templates/bds/forms/form.html
Normal file
22
bds/templates/bds/forms/form.html
Normal file
|
@ -0,0 +1,22 @@
|
|||
{% if errors %}
|
||||
{% if form.non_field_errors %}
|
||||
<div class="message is-danger">
|
||||
<div class="message-header">
|
||||
<button class="delete" aria-label="delete"></button>
|
||||
</div>
|
||||
<div class="message-body">
|
||||
{% for non_field_error in form.non_field_errors %}
|
||||
{{ non_field_error }}
|
||||
{% endfor %}
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
|
||||
{% for field in form.hidden_fields %}
|
||||
{{ field }}
|
||||
{% endfor %}
|
||||
|
||||
{% for field in form.visible_fields %}
|
||||
{% include 'bds/forms/field.html' %}
|
||||
{% endfor %}
|
19
bds/templates/bds/forms/input.html
Normal file
19
bds/templates/bds/forms/input.html
Normal file
|
@ -0,0 +1,19 @@
|
|||
{% load bulma_utils %}
|
||||
|
||||
<label class="label {% if field.field.required %}{{ form.required_css_class }}{% endif %}">
|
||||
{{ field.label }}
|
||||
</label>
|
||||
|
||||
<div class="control">
|
||||
{{ field|addclass:'input' }}
|
||||
|
||||
{% for error in field.errors %}
|
||||
<span class="help is-danger {{ form.error_css_class }}">{{ error }}</span>
|
||||
{% endfor %}
|
||||
|
||||
{% if field.help_text %}
|
||||
<p class="help">
|
||||
{{ field.help_text|safe }}
|
||||
</p>
|
||||
{% endif %}
|
||||
</div>
|
19
bds/templates/bds/forms/other.html
Normal file
19
bds/templates/bds/forms/other.html
Normal file
|
@ -0,0 +1,19 @@
|
|||
{% if field.auto_id %}
|
||||
<label class="label {% if field.field.required %}{{ form.required_css_class }}{% endif %}" for="{{ field.auto_id }}">
|
||||
{{ field.label }}
|
||||
</label>
|
||||
{% endif %}
|
||||
|
||||
<div class="control {% if field|is_multiple_checkbox %}multiple-checkbox{% endif %}">
|
||||
{{ field }}
|
||||
|
||||
{% for error in field.errors %}
|
||||
<span class="help is-danger {{ form.error_css_class }}">{{ error }}</span>
|
||||
{% endfor %}
|
||||
|
||||
{% if field.help_text %}
|
||||
<p class="help">
|
||||
{{ field.help_text|safe }}
|
||||
</p>
|
||||
{% endif %}
|
||||
</div>
|
24
bds/templates/bds/forms/radio.html
Normal file
24
bds/templates/bds/forms/radio.html
Normal file
|
@ -0,0 +1,24 @@
|
|||
{% if field.auto_id %}
|
||||
<label class="label {% if field.field.required %}{{ form.required_css_class }}{% endif %}">
|
||||
{{ field.label }}
|
||||
</label>
|
||||
{% endif %}
|
||||
|
||||
<div class="control">
|
||||
{% for choice in field %}
|
||||
<label class="radio">
|
||||
{{ choice.tag }}
|
||||
{{ choice.choice_label }}
|
||||
</label>
|
||||
{% endfor %}
|
||||
|
||||
{% for error in field.errors %}
|
||||
<span class="help is-danger {{ form.error_css_class }}">{{ error }}</span>
|
||||
{% endfor %}
|
||||
|
||||
{% if field.help_text %}
|
||||
<p class="help">
|
||||
{{ field.help_text|safe }}
|
||||
</p>
|
||||
{% endif %}
|
||||
</div>
|
21
bds/templates/bds/forms/select.html
Normal file
21
bds/templates/bds/forms/select.html
Normal file
|
@ -0,0 +1,21 @@
|
|||
{% load bulma_utils %}
|
||||
|
||||
<label class="label {% if field.field.required %}{{ form.required_css_class }}{% endif %}">
|
||||
{{ field.label }}
|
||||
</label>
|
||||
|
||||
<div class="control">
|
||||
<span class="select{% if field|is_multiple_select %} is-multiple{% endif %}{% if field.errors|length > 0 %} is-danger{% endif %}">
|
||||
{{ field }}
|
||||
</span>
|
||||
|
||||
{% for error in field.errors %}
|
||||
<span class="help is-danger {{ form.error_css_class }}">{{ error }}</span>
|
||||
{% endfor %}
|
||||
|
||||
{% if field.help_text %}
|
||||
<p class="help">
|
||||
{{ field.help_text|safe }}
|
||||
</p>
|
||||
{% endif %}
|
||||
</div>
|
17
bds/templates/bds/forms/textarea.html
Normal file
17
bds/templates/bds/forms/textarea.html
Normal file
|
@ -0,0 +1,17 @@
|
|||
<label class="label {% if field.field.required %}{{ form.required_css_class }}{% endif %}">
|
||||
{{ field.label }}
|
||||
</label>
|
||||
|
||||
<div class="control">
|
||||
{{ field|addclass:'textarea' }}
|
||||
|
||||
{% for error in field.errors %}
|
||||
<span class="help is-danger {{ form.error_css_class }}">{{ error }}</span>
|
||||
{% endfor %}
|
||||
|
||||
{% if field.help_text %}
|
||||
<p class="help">
|
||||
{{ field.help_text|safe }}
|
||||
</p>
|
||||
{% endif %}
|
||||
</div>
|
66
bds/templatetags/bulma_utils.py
Normal file
66
bds/templatetags/bulma_utils.py
Normal file
|
@ -0,0 +1,66 @@
|
|||
from django import forms, template
|
||||
|
||||
register = template.Library()
|
||||
|
||||
|
||||
@register.filter
|
||||
def widget_type(field):
|
||||
return field.field.widget
|
||||
|
||||
|
||||
@register.filter
|
||||
def is_select(field):
|
||||
return isinstance(field.field.widget, forms.Select)
|
||||
|
||||
|
||||
@register.filter
|
||||
def is_multiple_select(field):
|
||||
return isinstance(field.field.widget, forms.SelectMultiple)
|
||||
|
||||
|
||||
@register.filter
|
||||
def is_textarea(field):
|
||||
return isinstance(field.field.widget, forms.Textarea)
|
||||
|
||||
|
||||
@register.filter
|
||||
def is_input(field):
|
||||
return isinstance(
|
||||
field.field.widget,
|
||||
(
|
||||
forms.TextInput,
|
||||
forms.NumberInput,
|
||||
forms.EmailInput,
|
||||
forms.PasswordInput,
|
||||
forms.URLInput,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
@register.filter
|
||||
def is_checkbox(field):
|
||||
return isinstance(field.field.widget, forms.CheckboxInput)
|
||||
|
||||
|
||||
@register.filter
|
||||
def is_multiple_checkbox(field):
|
||||
return isinstance(field.field.widget, forms.CheckboxSelectMultiple)
|
||||
|
||||
|
||||
@register.filter
|
||||
def is_radio(field):
|
||||
return isinstance(field.field.widget, forms.RadioSelect)
|
||||
|
||||
|
||||
@register.filter
|
||||
def is_file(field):
|
||||
return isinstance(field.field.widget, forms.FileInput)
|
||||
|
||||
|
||||
@register.filter
|
||||
def addclass(field, css_class):
|
||||
if len(field.errors) > 0:
|
||||
css_class += " is-danger"
|
||||
field_classes = field.field.widget.attrs.get("class", "")
|
||||
field_classes += f" {css_class}"
|
||||
return field.as_widget(attrs={"class": field_classes})
|
Loading…
Reference in a new issue