This class is used to do some basic validation checks on the server side. If validation errors occur it is able to throw a ValidationException that can be consumed by the UI-part of the maydu GWT Validation Framework. Thus your interfaces that accept incoming data should declare that they throw a ValidationException in order to talk the news back to the client. You can use it like ('Fail late mode' aka
throwOnFirstError=false
):
ServerValidation validation = new ServerValidation(false); validation .notNull(g.getHomePlayer(), "homePlayer") .notNull(g.getAwayPlayer(), "awayPlayer") .notEqual(g.getAwayPlayer(), g.getHomePlayer(), "homePlayer,awayPlayer") .notNull(g.getHomeTeam(), "homeTeam") .notNull(g.getAwayTeam(), "awayTeam") .notEqual(g.getAwayTeam(), g.getHomeTeam(), "homeTeam,awayTeam") .notNull(score, "scores") .inRange(score.getHomeFirstHalf(), 0, 20, "homeFirstHalf") .inRange(score.getAwayFirstHalf(), 0, 20, "awayFirstHalf") .inRange(score.getHomeSecondHalf(), 0, 20, "homeSecondHalf") .inRange(score.getAwaySecondHalf(), 0, 20, "awaySecondHalf"); if(score.getHadOvertime()) { validation .inRange(score.getHomeFirstOvertime(), 0, 20, "homeFirstOvertime") .inRange(score.getAwayFirstOvertime(), 0, 20, "awayFirstOvertime") .inRange(score.getHomeSecondOvertime(), 0, 20, "homeSecondOvertime") .inRange(score.getAwaySecondOvertime(), 0, 20, "awaySecondOvertime"); if(score.getHadPenalty()) { validation .inRange(score.getHomePenalty(), 0, 99, "homePenalty") .inRange(score.getAwayPenalty(), 0, 99, "awayPenalty"); } } validation.validate();
Or like that in the 'fail fast mode' aka
throwOnFirstError=true
:
new ServerValidation() .notNull(g.getHomePlayer(), "homePlayer") .notNull(g.getAwayPlayer(), "awayPlayer") .notEqual(g.getAwayPlayer(), g.getHomePlayer(), "homePlayer,awayPlayer") .notNull(g.getHomeTeam(), "homeTeam") .notNull(g.getAwayTeam(), "awayTeam") .notEqual(g.getAwayTeam(), g.getHomeTeam(), "homeTeam,awayTeam") .notNull(score, "scores") .inRange(score.getHomeFirstHalf(), 0, 20, "homeFirstHalf") .inRange(score.getAwayFirstHalf(), 0, 20, "awayFirstHalf") .inRange(score.getHomeSecondHalf(), 0, 20, "homeSecondHalf") .inRange(score.getAwaySecondHalf(), 0, 20, "awaySecondHalf");
Notice that there is no need to call the
validate()
method at the end. Because it will directly fail when any of the validations fail. So if it goes through to the end, there were no validation errors. As you can see there are quite some ways you can validate the input from the client on the server side. If you still need additional checks that can't be covered by the existing functionality there is an easy way to still get the benefits of easily talking your validation errors back to the client and having him show the user that he did some wrong data entry: You just need to do your custom validation, if it fails you can talk back the error to the UI by invoking:
ServerValidation.exception("msgKey", "propertyName");
This creates a validation exception for you and throws it so the client UI can consume it. The 'msgKey' parameter defines the key to your own custom localized message. Which will on the client side result in the invocation of the
getCustomMessage(String)
method of the
ValidationMessages
class which you should extend and then override the
getCustomMessage(String)
method in order to return the correct localized string for the key: 'msgKey'. The 'propertyName' defines the name of the field that you gave it for validation that this validation error should be bound to.
@author Anatol Mayen