Skip to main content

Providing error messages

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

showLineNumbers {9,12,14,15,20}
1
import com.psddev.cms.db.Content;
2
import com.psddev.cms.db.ToolUi;
3
import com.psddev.dari.db.State;
4
5
public class Galaxy extends Content {
6
7
private String name;
8
9
@ToolUi.Note("(light years)")
10
private String distanceFromEarth;
11
12
protected void beforeSave() {
13
try {
14
Float.parseFloat(getDistance());
15
} catch (NumberFormatException e) {
16
State state = getState();
17
state.addError(
18
state.getField("distanceFromEarth"),
19
"Distance must be an integer (1234) or a floating-point number (1234.32)");
20
throw new IllegalArgumentException("Resolve the errors listed below, then click Publish.");
21
}
22
}
23
24
public String getDistance() {
25
return distanceFromEarth;
26
}
27
28
}
  • 9. Declares a field for entering a distance.
  • 12. Performs data validation prior to saving the object. Brightspot provides several callback methods with which you can perform validation.
  • 14. Checks if the editor entered a number.
  • 15. Displays a detailed error message above the field distanceFromEarth.
  • 20. Displays a general error message at the top of the content edit form.

Providing error messages.svg