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   
6   /**
7    * 指定した文字列と等しいかどうかを検証します。
8    * @author agata
9    *
10   */
11  public class EqualsValidator extends BaseValidator {
12  
13  	/**
14  	 * 対象文字列
15  	 */
16  	private final String value;
17  
18  	/**
19  	 * コンストラクタ
20  	 * @param value 対象文字列
21  	 */
22  	public EqualsValidator(final String value) {
23  		this(value, "valid.equals");
24  	}
25  
26  	/**
27  	 * エラーメッセージキーを指定するコンストラクタ
28  	 * @param value
29  	 * @param messageKey
30  	 */
31  	public EqualsValidator(final String value, final String messageKey) {
32  		this.value = value;
33  		this.setMessageKey(messageKey);
34  	}
35  
36  	public String validate(final ValidationContext ctx) {
37  		final Object value = ctx.getValue();
38  		if (this.value.equals(value)) {
39  			return null;
40  		} else {
41  			return getMessage(getPropertyMessage(ctx.getName()), this.value);
42  		}
43  	}
44  
45  }