Skip to main content

Form Fields

The structure and definition of fields within data models play a fundamental role in shaping the behavior and representation of data in your application. Understanding and customizing these fields are pivotal for precise data management and user interaction.

Defining ModelFields

Utilize ModelFields to specify the fields within your form. These fields directly correspond to predefined fields within your data model, automatically aligning with the appropriate form field types in the backend.

Customizing these fields involves specifying model field properties within your class to accurately mirror your data model. This customization enables defining types, formats, and other attributes for each field, ensuring a seamless transition from your model to the form representation.

Below is an example of a Patient form class:

class PatientForm(BaseForm):

name = ModelField(
placeholder="Enter Name",
required=True,
required_msg="This field is required."
)

contact_person = ModelField(
placeholder="Enter Contact Person",
required=True,
required_msg="This field is required.",
)

address = ModelField(
placeholder="Enter Address",
required=False
)

country = ModelField(
placeholder="Select Country",
required=True,
required_msg="This field is required.",
)

Defining Custom Fields

To define custom fields in YourModelForm class you can utilize CustomSchemaField. It enables complete customization of a field, allowing you to define extra_ui_schema and extra_schema for tailored field behavior. With this, you can provide additional configuration options beyond the default schema generated by utilizing from the form backend.

class PatientForm(BaseForm):

custom_field = CustomSchemaField(
required=True,
schema={
"type": "string",
"title": "Bio"
},
ui_schema={
"ui:widget": "TextareaFieldWidget"
}
)

char_custom_field = forms.CharField(label="Test Char Field", required=True)