mirror of https://github.com/snachodog/mybuddy.git
feat: create fieldset implementation
In order to have more control and flexiability on how elements are rendered create field set implementation. every field set gets rendered in order, and uses the template specified. This should probably be built into the metaclass of the form but i have no idea how to do that.
This commit is contained in:
parent
3edd645f53
commit
60b113f197
|
@ -4,10 +4,18 @@
|
|||
<div class="container-fluid pb-5">
|
||||
<form role="form" method="post" enctype="multipart/form-data">
|
||||
{% csrf_token %}
|
||||
{% if form.fieldsets %}
|
||||
{% for fieldset in form.hydrated_fielsets %}
|
||||
{% with "forms/layouts/"|add:fieldset.layout|add:".html" as template %}
|
||||
{% include template %}
|
||||
{% endwith %}
|
||||
{% endfor %}
|
||||
{% else %}
|
||||
{% for field in form %}
|
||||
{{ field.widget }}
|
||||
<div class="row">{% include 'babybuddy/form_field.html' %}</div>
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
<button type="submit" class="submit-primary btn btn-primary btn-lg">{% trans "Submit" %}</button>
|
||||
</form>
|
||||
</div>
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
{% load widget_tweaks %}
|
||||
{% if field|field_type == "booleanfield" %}
|
||||
{% if field.errors %}
|
||||
{{ field|add_class:"btn-check is-invalid" }}
|
||||
{% else %}
|
||||
{{ field|add_class:"btn-check" }}
|
||||
{% endif %}
|
||||
<label for="id_{{ field.name }}" class="btn btn-outline-light btn-no-hover">{{ field.label }}</label>
|
||||
{% elif 'choice' in field|field_type %}
|
||||
{% if field.errors %}
|
||||
{{ field|add_class:"form-select is-invalid" }}
|
||||
{% else %}
|
||||
{{ field|add_class:"form-select" }}
|
||||
{% endif %}
|
||||
{% else %}
|
||||
{% if field.errors %}
|
||||
{{ field|add_class:"form-control is-invalid" }}
|
||||
{% else %}
|
||||
{{ field|add_class:"form-control" }}
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
{% if field.help_text %}
|
||||
<div class="help-block">
|
||||
<small>{{ field.help_text }}</small>
|
||||
</div>
|
||||
{% endif %}
|
||||
{% if field.errors %}
|
||||
<div class="invalid-feedback">
|
||||
{% for error in field.errors %}{{ error }}{% endfor %}
|
||||
</div>
|
||||
{% endif %}
|
|
@ -107,6 +107,31 @@ class CoreModelForm(forms.ModelForm):
|
|||
self.save_m2m()
|
||||
return instance
|
||||
|
||||
@property
|
||||
def hydrated_fielsets(self):
|
||||
# for some reason self.fields returns defintions and not bound fields
|
||||
# so until i figure out a better way we can just create a dict here
|
||||
# https://github.com/django/django/blob/main/django/forms/forms.py#L52
|
||||
|
||||
bound_field_dict = {}
|
||||
for field in self:
|
||||
bound_field_dict[field.name] = field
|
||||
|
||||
hydrated_fieldsets = []
|
||||
|
||||
for fieldset in self.fieldsets:
|
||||
hyrdrated_fieldset = {
|
||||
"layout": fieldset["layout"],
|
||||
"layout_attrs": fieldset.get("layout_attrs", {}),
|
||||
"fields": [],
|
||||
}
|
||||
for field_name in fieldset["fields"]:
|
||||
hyrdrated_fieldset["fields"].append(bound_field_dict[field_name])
|
||||
|
||||
hydrated_fieldsets.append(hyrdrated_fieldset)
|
||||
|
||||
return hydrated_fieldsets
|
||||
|
||||
|
||||
class ChildForm(forms.ModelForm):
|
||||
class Meta:
|
||||
|
|
Loading…
Reference in New Issue