Skip to main content

Creating Custom form fields

The library automatically generates a form based on Java entity classes. It automatically generates form fields (e.g., TextField, DateField, SearchableSelectField, etc.) based on entity attributes and annotations. It defaults to a TextField ie

 <input type="text" name="" id="">

To generate a custom input type like image field, you can annotate your entity field with @FormInputType.The annotation @FormInputType allows you to define how a field should be rendered on the admin form:


@Entity
public class Product {
@Id
private UUID id;

// renders input type text
@FormInputType(FormInputType.Type.TEXT)
private String name;

// renders intype type number
@FormInputType(FormInputType.Type.NUMBER)
private Double price;

// renders input type file
@FormInputType(FormInputType.Type.IMAGE)
private String thumbnail;

// renders quill editor
@FormInputType(FormInputType.Type.WYSIWYG)
private String description;

// getters & setters...
}

This means you can declaratively control whether a field appears as a textbox, number input, image uploader, WYSIWYG editor, etc., just by annotating the field.

Kraft Admin will automatically reflect this in the UI form without extra frontend code.

Supported form fields

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface FormInputType {
Type value(); // type of input field

enum Type {
TEXT, NUMBER, COLOR, CHECKBOX, IMAGE, DATE, EMAIL, PASSWORD, FILE, TEXTAREA, WYSIWYG, DATETIME, TIME, RANGE, TEL, URL, RADIO, CURRENCY
}
}