Support and Documentation

Providing error messages

Brightspot provides convenient APIs for displaying data validation and other error messages on the content edit form.

import com.psddev.cms.db.Content;
import com.psddev.cms.db.ToolUi;
import com.psddev.dari.db.State;

public class Galaxy extends Content {

    private String name;

    @ToolUi.Note("(light years)") 1
    private String distanceFromEarth;

    protected void beforeSave() { 2
        try {
            Float.parseFloat(getDistance()); 3
        } catch (NumberFormatException e) { 4
            State state = getState();
            state.addError(
                 state.getField("distanceFromEarth"),
                 "Distance must be an integer (1234) or a floating-point number (1234.32)");
            throw new IllegalArgumentException("Resolve the errors listed below, then click Publish."); 5
        }
    }

    public String getDistance() {
        return distanceFromEarth;
    }

}

1

Declares a field for entering a distance.

2

Performs data validation prior to saving the object. Brightspot provides several callback methods with which you can perform validation.

3

Checks if the editor entered a number.

4

Displays a detailed error message above the field distanceFromEarth.

5

Displays a general error message at the top of the content edit form.

content-edit-form-error-messages.svg