# 10.2 Validator

# 1. Basic Usage

The Validator class implements the Interceptor interface, making it an interceptor as well. Its configuration is identical to that of other interceptors. Here's a basic example:

public class LoginValidator extends Validator {
    protected void validate(Controller c) {
       validateRequiredString("name", "nameMsg", "Please enter your username");
       validateRequiredString("pass", "passMsg", "Please enter your password");
    }
    protected void handleError(Controller c) {
       c.keepPara("name");
       c.render("login.html");
    }
}
1
2
3
4
5
6
7
8
9
10

In the validate(Controller c) method, you can call various validateXxx(...) methods for server-side validation. The handleError(Controller c) method allows you to call c.keepPara(...) to retain submitted values and send them back to the page. This method is only invoked when validation fails.

The handleError method uses keepXxx methods to retain the data from the form and pass it back to the page, which saves the user from re-entering form fields that have already been validated.

For model objects, you can use the keepModel(...) method, and for traditional Java bean objects, you can use keepBean(...).

The keepPara(...) method keeps all data as a String by default. If you wish to keep them in a specific type, you can use keepPara(Class, …).

# 2. setRet(...) and getRet()

JFinal 4.0 introduced the setRet(Ret) and getRet() methods. Here's a typical example:

public class LoginValidator extends Validator {
 
   protected void validate(Controller c) {
      setRet(Ret.fail());
      	
      validateRequired("userName", "msg", "Email can't be empty");
      validateEmail("userName", "msg", "Email format is incorrect");
      validateRequired("password", "msg", "Password can't be empty");
      validateCaptcha("captcha", "msg", "Captcha is incorrect");
   }
   	
   protected void handleError(Controller c) {
      c.renderJson(getRet());
   }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

In this example, setRet(Ret.fail()) injects a Ret object into the LoginValidator. Subsequent validateXxx methods will store all validation results in this Ret object. In the handleError method, c.renderJson(getRet()) retrieves and renders this Ret object.

# 3. Advanced Usage

Though the Validator class offers a rich set of validateXxx(...) methods, there are limits. If these methods don't meet your needs, you can use validateRegex(...) or ordinary Java code:

protected void validate(Controller c) {
    String nickName = c.getPara("nickName");
    if (userService.isExists(nickName)) {
        addError("msg", "Nickname is already taken. Please choose another one.");
    }
}
1
2
3
4
5
6

In this example, common Java code combined with addError(...) allows for unlimited, flexible custom validation.

You can also set the validator to skip remaining validations after encountering the first failure:

protected void validate(Controller c) {
    this.setShortCircuit(true);
    ....
}
1
2
3
4

setShortCircuit(boolean) sets the validation mode to "short-circuit," meaning it will skip the rest of the validations if one fails.

Last Updated: 9/22/2023, 7:24:41 AM