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.
showLineNumbers {9,12,14,15,20}
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;1112protected void beforeSave() {13try {14Float.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)");20throw new IllegalArgumentException("Resolve the errors listed below, then click Publish.");21}22}2324public String getDistance() {25return distanceFromEarth;26}2728}
- 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.