Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion crudadmin/admin_interface/model_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,24 @@ async def form_create_endpoint_inner(
for field in form_fields:
key = field["name"]
raw_value = form_data_raw.getlist(key)
if len(raw_value) == 1:

if field["type"] == "checkbox":
if raw_value and len(raw_value) == 1:
value_str = raw_value[0]
if value_str == "true":
form_data[key] = True
field_values[key] = True
elif value_str == "false":
form_data[key] = False
field_values[key] = False
else:
form_data[key] = bool(value_str)
field_values[key] = bool(value_str)
else:
has_default = field.get("default") is not None
form_data[key] = None if has_default else False
field_values[key] = None if has_default else False
elif len(raw_value) == 1:
value = raw_value[0]
form_data[key] = value if value else field.get("default")
field_values[key] = value
Expand Down Expand Up @@ -1163,6 +1180,27 @@ async def form_update_endpoint_inner(
update_data: Dict[str, Any] = {}
has_updates = False

for field in form_fields:
key = field["name"]
if field["type"] == "checkbox":
raw_values = form_data.getlist(key)
if raw_values and len(raw_values) == 1:
value_str = raw_values[0]
if value_str == "true":
update_data[key] = True
field_values[key] = True
elif value_str == "false":
update_data[key] = False
field_values[key] = False
else:
update_data[key] = bool(value_str)
field_values[key] = bool(value_str)
has_updates = True
elif field.get("default") is None:
update_data[key] = False
field_values[key] = False
has_updates = True

for key, raw_val in form_data.items():
if isinstance(raw_val, UploadFile):
field_values[key] = raw_val
Expand Down
58 changes: 48 additions & 10 deletions crudadmin/templates/admin/model/create.html
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,25 @@
gap: 0.5rem;
}

.create-form-container .radio-group {
display: flex;
gap: 1.5rem;
margin-top: 0.5rem;
}

.create-form-container .radio-option {
display: flex;
align-items: center;
gap: 0.5rem;
cursor: pointer;
font-weight: normal;
}

.create-form-container .radio-option input[type="radio"] {
cursor: pointer;
width: auto;
}

.create-form-container .form-control[type="number"] {
-moz-appearance: none;
}
Expand Down Expand Up @@ -298,16 +317,35 @@ <h1 class="page-title">Create {{ model_name }}</h1>
</select>

{% elif field.type == "checkbox" %}
<div class="checkbox-wrapper">
<input class="form-control{% if field.name in field_errors %} error{% endif %}"
type="checkbox"
id="{{ field.name }}"
name="{{ field.name }}"
{% if field_values and field_values[field.name] %}checked{% endif %}>
{% if field.description %}
<span class="checkbox-label">{{ field.description }}</span>
{% endif %}
</div>
{% if field.default is not none %}
<div class="radio-group">
<label class="radio-option">
<input type="radio"
name="{{ field.name }}"
value="true"
{% if field_values and field_values[field.name] == True %}checked{% elif not field_values and field.default == True %}checked{% endif %}>
<span>True</span>
</label>
<label class="radio-option">
<input type="radio"
name="{{ field.name }}"
value="false"
{% if field_values and field_values[field.name] == False %}checked{% elif not field_values and field.default == False %}checked{% endif %}>
<span>False</span>
</label>
</div>
{% else %}
<div class="checkbox-wrapper">
<input class="form-control{% if field.name in field_errors %} error{% endif %}"
type="checkbox"
id="{{ field.name }}"
name="{{ field.name }}"
{% if field_values and field_values[field.name] %}checked{% endif %}>
{% if field.description %}
<span class="checkbox-label">{{ field.description }}</span>
{% endif %}
</div>
{% endif %}

{% elif field.type == "json" %}
<textarea class="form-control{% if field.name in field_errors %} error{% endif %}"
Expand Down
58 changes: 48 additions & 10 deletions crudadmin/templates/admin/model/update.html
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,25 @@
gap: 0.5rem;
}

.radio-group {
display: flex;
gap: 1.5rem;
margin-top: 0.5rem;
}

.radio-option {
display: flex;
align-items: center;
gap: 0.5rem;
cursor: pointer;
font-weight: normal;
}

.radio-option input[type="radio"] {
cursor: pointer;
width: auto;
}

.form-control[type="number"] {
-moz-appearance: none;
}
Expand Down Expand Up @@ -310,16 +329,35 @@ <h1 class="page-title">Update {{ model_name }}</h1>
</select>

{% elif field.type == "checkbox" %}
<div class="checkbox-wrapper">
<input class="form-control{% if field.name in field_errors %} error{% endif %}"
type="checkbox"
id="{{ field.name }}"
name="{{ field.name }}"
{% if field_values and field_values[field.name] %}checked{% endif %}>
{% if field.description %}
<span class="checkbox-label">{{ field.description }}</span>
{% endif %}
</div>
{% if field.default is not none %}
<div class="radio-group">
<label class="radio-option">
<input type="radio"
name="{{ field.name }}"
value="true"
{% if field_values and field_values[field.name] == True %}checked{% endif %}>
<span>True</span>
</label>
<label class="radio-option">
<input type="radio"
name="{{ field.name }}"
value="false"
{% if field_values and field_values[field.name] == False %}checked{% endif %}>
<span>False</span>
</label>
</div>
{% else %}
<div class="checkbox-wrapper">
<input class="form-control{% if field.name in field_errors %} error{% endif %}"
type="checkbox"
id="{{ field.name }}"
name="{{ field.name }}"
{% if field_values and field_values[field.name] %}checked{% endif %}>
{% if field.description %}
<span class="checkbox-label">{{ field.description }}</span>
{% endif %}
</div>
{% endif %}

{% elif field.type == "json" %}
<textarea class="form-control{% if field.name in field_errors %} error{% endif %}"
Expand Down
Loading