mirror of
https://github.com/inventree/InvenTree.git
synced 2025-12-14 08:19:54 +00:00
Fix for string form fields (#10814)
* Fix for string form fields - replace null values with empty strings * Expose more serializer metadata * Check if null values are not allowed * Fix type * Try removing feature * Reduce deltas * Remove extra field attrs entirely (for testing) * Comment out changes * Tweak form values * Fix for form validation
This commit is contained in:
@@ -420,6 +420,13 @@ class InvenTreeMetadata(SimpleMetadata):
|
|||||||
if field_info['type'] == 'dependent field':
|
if field_info['type'] == 'dependent field':
|
||||||
field_info['depends_on'] = field.depends_on
|
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
|
# Extend field info if the field has a get_field_info method
|
||||||
if (
|
if (
|
||||||
not field_info.get('read_only')
|
not field_info.get('read_only')
|
||||||
|
|||||||
@@ -48,6 +48,8 @@ export type ApiFormFieldHeader = {
|
|||||||
* @param model : The model to use for related fields
|
* @param model : The model to use for related fields
|
||||||
* @param filters : Optional API filters to apply to related fields
|
* @param filters : Optional API filters to apply to related fields
|
||||||
* @param required : Whether the field is required
|
* @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 hidden : Whether the field is hidden
|
||||||
* @param disabled : Whether the field is disabled
|
* @param disabled : Whether the field is disabled
|
||||||
* @param error : Optional error message to display
|
* @param error : Optional error message to display
|
||||||
@@ -103,6 +105,8 @@ export type ApiFormFieldType = {
|
|||||||
choices?: ApiFormFieldChoice[];
|
choices?: ApiFormFieldChoice[];
|
||||||
hidden?: boolean;
|
hidden?: boolean;
|
||||||
disabled?: boolean;
|
disabled?: boolean;
|
||||||
|
allow_null?: boolean;
|
||||||
|
allow_blank?: boolean;
|
||||||
exclude?: boolean;
|
exclude?: boolean;
|
||||||
read_only?: boolean;
|
read_only?: boolean;
|
||||||
placeholder?: string;
|
placeholder?: string;
|
||||||
|
|||||||
@@ -24,7 +24,11 @@ import { type NavigateFunction, useNavigate } from 'react-router-dom';
|
|||||||
|
|
||||||
import { isTrue } from '@lib/functions/Conversion';
|
import { isTrue } from '@lib/functions/Conversion';
|
||||||
import { getDetailUrl } from '@lib/functions/Navigation';
|
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 { useApi } from '../../contexts/ApiContext';
|
||||||
import {
|
import {
|
||||||
type NestedDict,
|
type NestedDict,
|
||||||
@@ -375,16 +379,29 @@ export function ApiForm({
|
|||||||
|
|
||||||
Object.keys(data).forEach((key: string) => {
|
Object.keys(data).forEach((key: string) => {
|
||||||
let value: any = data[key];
|
let value: any = data[key];
|
||||||
const field_type = fields[key]?.field_type;
|
const field: ApiFormFieldType = fields[key] ?? {};
|
||||||
const exclude = fields[key]?.exclude;
|
const field_type = field?.field_type;
|
||||||
|
const exclude = field?.exclude;
|
||||||
|
|
||||||
if (field_type == 'file upload' && !!value) {
|
if (field_type == 'file upload' && !!value) {
|
||||||
hasFiles = true;
|
hasFiles = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure any boolean values are actually boolean
|
// Special consideration for various field types
|
||||||
if (field_type === 'boolean') {
|
switch (field_type) {
|
||||||
value = isTrue(value) || false;
|
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 = '';
|
||||||
|
jsonData[key] = value;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stringify any JSON objects
|
// Stringify any JSON objects
|
||||||
@@ -393,7 +410,9 @@ export function ApiForm({
|
|||||||
case 'file upload':
|
case 'file upload':
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
value = JSON.stringify(value);
|
if (value !== null && value !== undefined) {
|
||||||
|
value = JSON.stringify(value);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user