Skip to content
Open
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
7 changes: 7 additions & 0 deletions src/backend/InvenTree/InvenTree/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,13 @@ def get_field_info(self, field):
if field_info['type'] == 'dependent field':
field_info['depends_on'] = field.depends_on

# Extends with extra attributes from the serializer
extra_field_attributes = ['allow_blank', 'allow_null']

for attr in extra_field_attributes:
if hasattr(field, attr):
field_info[attr] = getattr(field, attr)

# Extend field info if the field has a get_field_info method
if (
not field_info.get('read_only')
Expand Down
4 changes: 4 additions & 0 deletions src/frontend/lib/types/Forms.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ export type ApiFormFieldHeader = {
* @param model : The model to use for related fields
* @param filters : Optional API filters to apply to related fields
* @param required : Whether the field is required
* @param allow_null: Whether the field allows null values
* @param allow_blank: Whether the field allows blank values
* @param hidden : Whether the field is hidden
* @param disabled : Whether the field is disabled
* @param error : Optional error message to display
Expand Down Expand Up @@ -103,6 +105,8 @@ export type ApiFormFieldType = {
choices?: ApiFormFieldChoice[];
hidden?: boolean;
disabled?: boolean;
allow_null?: boolean;
allow_blank?: boolean;
exclude?: boolean;
read_only?: boolean;
placeholder?: string;
Expand Down
33 changes: 26 additions & 7 deletions src/frontend/src/components/forms/ApiForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ import { type NavigateFunction, useNavigate } from 'react-router-dom';

import { isTrue } from '@lib/functions/Conversion';
import { getDetailUrl } from '@lib/functions/Navigation';
import type { ApiFormFieldSet, ApiFormProps } from '@lib/types/Forms';
import type {
ApiFormFieldSet,
ApiFormFieldType,
ApiFormProps
} from '@lib/types/Forms';
import { useApi } from '../../contexts/ApiContext';
import {
type NestedDict,
Expand Down Expand Up @@ -375,16 +379,28 @@ export function ApiForm({

Object.keys(data).forEach((key: string) => {
let value: any = data[key];
const field_type = fields[key]?.field_type;
const exclude = fields[key]?.exclude;
const field: ApiFormFieldType = fields[key] ?? {};
const field_type = field?.field_type;
const exclude = field?.exclude;

if (field_type == 'file upload' && !!value) {
hasFiles = true;
}

// Ensure any boolean values are actually boolean
if (field_type === 'boolean') {
value = isTrue(value) || false;
// Special consideration for various field types
switch (field_type) {
case 'boolean':
// Ensure boolean values are actually boolean
value = isTrue(value) || false;
break;
case 'string':
// Replace null string values with an empty string
if (value === null && field?.allow_null == false) {
value = '';
}
break;
default:
break;
}

// Stringify any JSON objects
Expand All @@ -393,7 +409,9 @@ export function ApiForm({
case 'file upload':
break;
default:
value = JSON.stringify(value);
if (value !== null && value !== undefined) {
// value = JSON.stringify(value);
}
break;
}
}
Expand All @@ -402,6 +420,7 @@ export function ApiForm({
// Remove the field from the data
delete jsonData[key];
} else if (value != undefined) {
// jsonData[key] = value;
formData.append(key, value);
}
});
Expand Down
Loading