Skip to content

Instantly share code, notes, and snippets.

@xnrghzjh
Created October 13, 2011 00:16
Show Gist options
  • Save xnrghzjh/1283019 to your computer and use it in GitHub Desktop.
Save xnrghzjh/1283019 to your computer and use it in GitHub Desktop.
Hibernate Validator を使ったバリデーション
public class BeansValidator {
private ClassValidator classValidator;
private List<String> validMessages = new ArrayList<String>();
private BeansValidator() {}
public BeansValidator(Class clazz) throws Exception{
classValidator = new ClassValidator(clazz);
// ルールが設定されていなければ例外を発生して通知
if (!classValidator.hasValidationRules()){
throw new Exception();// とりあえずで、NotFoundValidationRulesExceptionとか?
}
}
public Boolean validate(Object obj) {
validMessages.clear();
for (InvalidValue value : classValidator.getInvalidValues(obj)) {
String message = MessageFormat.format("{0}:{1}",
value.getValue(),
value.getMessage());
validMessages.add(message);
}
return validMessages.isEmpty();
}
/**
* @return the validMessages
*/
public List<String> getValidMessages() {
return validMessages;
}
}
import org.hibernate.validator.ClassValidator;
import org.hibernate.validator.InvalidValue;
ValidBeans validBeans = new ValidBeans();
validBeans.setStringNull(null);
validBeans.setLengthOver("12345678901");
validBeans.setLengthUnder("0123");
validBeans.setValueNull(null);
validBeans.setValueOver(101);
validBeans.setValueUnder(99);
try {
BeansValidator validator = new BeansValidator(ValidBeans.class);
if (!validator.validate(validBeans)) {
for (String message : validator.getValidMessages()) {
System.out.println(message);
}
}
} catch (Exception e) {
// 例外処理
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Develop;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import org.hibernate.validator.Length;
import org.hibernate.validator.Max;
import org.hibernate.validator.Min;
import org.hibernate.validator.NotNull;
/**
*
* @author xnrghzjh
*/
public class ValidBeans{
@Id
@GeneratedValue (strategy=GenerationType.AUTO)
@Column(name="Key")
private Integer key;
@NotNull(message="空文字列は入力できません")
@Column(name="stringNull")
private String stringNull;
@Length(min=0, max=10, message="文字列が長すぎます")
@Column(name="LengthOver")
private String lengthOver;
@Length(min=10, max=15, message="文字列が短すぎます")
@Column(name="LengthUnder")
private String lengthUnder;
@NotNull(message="値を入力してください")
@Column(name="ValueNull")
private Integer valueNull;
@Max(value=100, message="数値が大きすぎます")
@Column(name="ValueOver")
private Integer valueOver;
@Min(value=100, message="数値が小さすぎます")
@Column(name="ValueUnder")
private Integer valueUnder;
/**
* @return the key
*/
public Integer getKey() {
return key;
}
/**
* @param key the key to set
*/
public void setKey(Integer key) {
this.key = key;
}
/**
* @return the stringNull
*/
public String getStringNull() {
return stringNull;
}
/**
* @param stringNull the stringNull to set
*/
public void setStringNull(String stringNull) {
this.stringNull = stringNull;
}
/**
* @return the lengthOver
*/
public String getLengthOver() {
return lengthOver;
}
/**
* @param lengthOver the lengthOver to set
*/
public void setLengthOver(String lengthOver) {
this.lengthOver = lengthOver;
}
/**
* @return the lengthUnder
*/
public String getLengthUnder() {
return lengthUnder;
}
/**
* @param lengthUnder the lengthUnder to set
*/
public void setLengthUnder(String lengthUnder) {
this.lengthUnder = lengthUnder;
}
/**
* @return the valueNull
*/
public Integer getValueNull() {
return valueNull;
}
/**
* @param valueNull the valueNull to set
*/
public void setValueNull(Integer valueNull) {
this.valueNull = valueNull;
}
/**
* @return the valueOver
*/
public Integer getValueOver() {
return valueOver;
}
/**
* @param valueOver the valueOver to set
*/
public void setValueOver(Integer valueOver) {
this.valueOver = valueOver;
}
/**
* @return the valueUnder
*/
public Integer getValueUnder() {
return valueUnder;
}
/**
* @param valueUnder the valueUnder to set
*/
public void setValueUnder(Integer valueUnder) {
this.valueUnder = valueUnder;
}
}
null:空文字列は入力できません
12345678901:文字列が長すぎます
0123:文字列が短すぎます
null:値を入力してください
101:数値が大きすぎます
99:数値が小さすぎます
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment