Utilizing DocField Feature for Efficient Empress Application Development

Introduction

In the realm of Empress, one feature stands out for its ability to define the properties or fields of a DocType - the DocField. This feature is essential for developers as it allows them to customize the behavior of fields, thus enhancing the overall functionality of their applications.

What is a DocField?

A DocField is a powerful feature that defines a property or a field of a DocType. It allows you to specify the column name, label, datatype, and more for DocFields. For instance, a ToDo doctype might have fields such as description, status, and priority. These fields ultimately become columns in the database table, like tabToDo.

The DocField in Action

To truly understand the potential of the DocField, let’s look at an example. The DocField stores meta-data about the field, such as the label, fieldname, fieldtype, and more.

[
    {
        "label": "Description",     // the value shown to the user (Form, Print, etc)
        "fieldname": "description", // the property name we refer in code, also the column name
        "fieldtype": "Text Editor", // the fieldtype which also decides how to store this value
        "reqd": 1                   // whether this field is mandatory
    },
    {
        "label": "Status",
        "fieldname": "status",
        "fieldtype": "Select",
        "options": [
            "Open",
            "Pending",
            "Closed"
        ]
    },
    // ... more fields ...
]

In the above example, the “Description” DocField is of type “Text Editor”, is mandatory ("reqd": 1), and its value is shown to the user in various places like the Form and Print views.

New DocField Properties in Version 12

Version 12 of Empress introduces two new properties:

  • mandatory_depends_on: The field will be mandatory if this condition is satisfied.
  • read_only_depends_on: The field will be read-only if this condition is satisfied.

These properties add new levels of control over your DocFields, allowing you to set conditions on whether a field should be mandatory or read-only.

Fieldtypes in Empress

Empress comes with over 30 different fieldtypes out-of-the-box, providing a variety of use-cases for your application. You will learn more about these fieldtypes in the next section of this guide.

Conclusion

The DocField feature is a versatile tool in the developer’s arsenal, allowing for detailed customization of fields and enhancing the overall application experience. By defining property attributes and conditions, developers can create dynamic and responsive applications that adapt to users’ needs. With the addition of new properties in Version 12, developers have even more control and flexibility in defining their DocTypes, further solidifying the DocField’s integral role in Empress software development and customization.