Skip to content

Form Validation (validate) 1.13.1+ ​

validate is triggered before add and update operations are saved, and is dedicated to data validation: throw EruptException (or its subclass EruptApiErrorTip) to reject the operation and show the error message to the user.

Code Example ​

java
@Service
public class EruptTestDataProxy implements DataProxy<EruptTest> {

    @Override
    public void validate(EruptTest model) throws EruptException {
        if ("John".equals(model.getName())) {
            throw new EruptApiErrorTip("Name 'John' is not allowed!");
        }
        if (model.getEndDate().before(model.getStartDate())) {
            throw new EruptApiErrorTip("End date cannot be earlier than start date");
        }
    }

}

validate() vs before* Methods ​

validate()beforeAdd() / beforeUpdate()
PurposeData validation — reject the operation if it failsModify data or execute business logic before saving
Since1.13.1+All versions
Recommended forFormat checks, business rule validationAuto-filling fields, calling external services
java
@Override
public void validate(EruptTest model) throws EruptException {
    // Recommended: pure validation logic, do not modify data here
    if (model.getEndDate().before(model.getStartDate())) {
        throw new EruptApiErrorTip("End date cannot be earlier than start date");
    }
}

@Override
public void beforeAdd(EruptTest model) {
    // Recommended: modify or supplement data
    model.setCreateBy(getCurrentUser());
}

Server-side Enforcement of Declared Limits v2.4.0+ ​

Since 2.4.0 the limits an edit form declares in its annotations are enforced on the server as well (EruptValueValidator), not only by the page widgets: values arriving through a direct API call, a cell edit, a batch edit or an Excel import are checked against the same rules, and a failure names the field and the offending value.

ComponentWhat is checked
INPUTlength within @InputType(length)
NUMBER@NumberType(min / max), only where explicitly declared
SLIDER@SliderType(min / max)
RATE0 .. count, halves only with allowHalf = true
TAGSno more than maxTagCount; with allowExtension = false every tag must come from tags or fetchHandler
DATEthe direction of pickerMode FUTURE / HISTORY, and the min / max range
CHOICE / MULTI_CHOICEthe value must be one of the options, see Server-side Option Check
TAB_TABLE_ADDevery row runs its sub model's own rules: required, regex, XSS, the sub model's DataProxy.validate, then the table above
  • Only fields present in the submitted data are checked, so a cell edit checks its one field
  • On an edit a value equal to the stored one passes: data saved before a rule tightened (a retired option, a lowered maximum) never blocks editing the rest of that record
  • This layer runs before DataProxy.validate; business rules still belong in validate
java
@EruptField(
    edit = @Edit(title = "Discount", type = EditType.NUMBER,
                 numberType = @NumberType(min = 0, max = 100))
)
private Integer discount; // an API call submitting 120 is refused too

Contributors

The avatar of contributor named as YuePeng YuePeng

Changelog

Released under the Apache-2.0 License.