Creating Form Field Types

Liferay’s Forms application does not contain a dedicated time field out-of-the-box. For ease of use and to ensure proper time data is collected, you’ll develop a time field and learn how Liferay DXP’s field types work at the same time.

There are several steps involved in creating a form field type:

  1. Creating the Form Field Type’s Java class.

  2. Creating the Form Field Type Renderer Java class.

  3. Defining the field’s behavior in JavaScript and Soy templates.

Start by setting up the project’s metadata.

Specifying OSGi Metadata

First specify the necessary OSGi metadata in a bnd.bnd file (see here for more information). Here’s what it would look like for a module in a folder called dynamic-data-mapping-type-time:

Bundle-Name: Liferay Dynamic Data Mapping Type Time
Bundle-Version: 1.0.0
Liferay-JS-Config: /META-INF/resources/config.js
Web-ContextPath: /dynamic-data-mapping-type-time

Point to the JavaScript configuration file (config.js) that defines JavaScript modules added by your module (you’ll get to that later) and set the Web Context Path to the modules root folder, so your module’s resources are made available upon module activation.

Next craft the OSGi Component that marks your class as an implementation of DDMFormFieldType.

Creating a DDMFormFieldType Component

If you’re creating a Time field type, define the Component at the top of your *DDMFormFieldType class like this:

  immediate = true,
  property = {
  service = DDMFormFieldType.class

Define the field type’s properties (property=...) and declare that you’re implementing the DDMFormFieldType service (service=...).

DDMFormFieldType Components can have several properties:

An optional property describing the field type. Its localized value appears in the form builder’s sidebar, just below the field’s label.
An Integer defining the field type’s position in the sidebar.
The icon for the field type. Choosing one of the Lexicon icons makes your form field blend in with the existing form field types.
The field type’s JavaScript class name—the JavaScript file defines the field type’s behavior.
The name of the JavaScript module provided to the Form engine so the module can be loaded when needed.
The field type’s label. Its localized value appears in the form builder’s sidebar.
The field type’s name must be unique. Each Component in a field type module references the field type name, and it’s used by OSGi service trackers to filter the field’s capabilities (for example, rendering and validation).

Next code the *DDMFormFieldType class.

Implementing DDMFormFieldType

Implementing the field type in Java is made easier because of BaseDDMFormFieldType, an abstract class you can leverage in your code.

After extending BaseDDMFormFieldType, override the getName method by specifying the name of your new field type:

public class TimeDDMFormFieldType extends BaseDDMFormFieldType {

    public String getName() {
        return "time";


That’s all there is to defining the field type. Next determine how your field type is rendered.

« Form Field TypesRendering Field Types »
¿Fue útil este artículo?
Usuarios a los que les pareció útil: 1 de 1