Writing Validations

When implementing a system, we want to validate user input. This is usually done using custom code in the Logic layer. The Bean for a specific data element will have a pre-create anchor, which will allow a developer to inject custom code. The following example validates a licence plate using a simple regex:

-- anchor:custom-imports:start
import net.democritus.sys.DiagnosticFieldFactory;
-- anchor:custom-imports:end
-- anchor:custom-preCreate:start
        String licensePlate = details.getLicensePlate();
        if (!licensePlate.matches("[A-Z0-9\\-]+")) {
            DiagnosticFieldFactory fieldFactory = diagnosticFactory.fieldFactory("licensePlate");
            Diagnostic diagnostic = fieldFactory.error("rentWork.car.licensePlate.unexpectedSymbol");
            return diagnosticHelper.createCrudsError(diagnostic);
        }
-- anchor:custom-preCreate:end
-- anchor:custom-preModify:start
        String licensePlate = details.getLicensePlate();
        if (!licensePlate.matches("[A-Z0-9\\-]+")) {
            Diagnostic diagnostic = diagnosticFactory.fieldFactory("licensePlate").error("rentWork.car.licensePlate.unexpectedSymbol");
            return diagnosticHelper.createCrudsError(diagnostic);
        }
-- anchor:custom-preModify:end

The result is wrapped in a CrudsResult class.