View Javadoc

1   package org.seasar.cubby.validator.validators;
2   
3   import org.seasar.cubby.validator.BaseValidator;
4   import org.seasar.cubby.validator.ValidationContext;
5   import org.seasar.framework.util.StringUtil;
6   
7   /**
8    * 必須検証します。<p>
9    * 文字列の長さが0の場合、検証エラーとなります。
10   * @author agata
11   *
12   */
13  public class RequiredValidator extends BaseValidator {
14  
15  	/**
16  	 * コンストラクタ
17  	 */
18  	public RequiredValidator() {
19  		this("valid.required");
20  	}
21  
22  	/**
23  	 * エラーメッセージキーを指定するコンストラクタ
24  	 * @param messageKey エラーメッセージキー
25  	 */
26  	public RequiredValidator(final String messageKey) {
27  		this.setMessageKey(messageKey);
28  	}
29  
30  	public String validate(final ValidationContext ctx) {
31  		final Object value = ctx.getValue();
32  		if (value instanceof String) {
33  			String str = (String)value;
34  			if (!StringUtil.isEmpty(str)) {
35  				return null;
36  			}
37  		} else if (value != null) {
38  			return null;
39  		}
40  		return getMessage(getPropertyMessage(ctx
41  				.getName()));
42  	}
43  
44  }