Providing error messages
Brightspot provides convenient APIs for displaying data validation and other error messages on the content edit form.
- Use IllegalArgumentException to display error messages at the top of the content edit form.
- Use State#addError to display error messages above a field.
1import com.psddev.cms.db.Content;2import com.psddev.cms.db.ToolUi;3import com.psddev.dari.db.State;45public class Galaxy extends Content {67private String name;89①@ToolUi.Note("(light years)")10private String distanceFromEarth;1112②protected void beforeSave() {13try {14③Float.parseFloat(getDistance());15④} catch (NumberFormatException e) {16State state = getState();17state.addError(18state.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}2324public String getDistance() {25return distanceFromEarth;26}2728}
- ①Declares a field for entering a distance.
- ②Performs data validation prior to saving the object. Brightspot provides several callback methods with which you can perform validation.
- ③Checks if the editor entered a number.
- ④Displays a detailed error message above the field
distanceFromEarth. - ⑤Displays a general error message at the top of the content edit form.