package example.u2ware.springfield.part4.step1;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.Type;
import org.joda.time.DateTime;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.NumberFormat;
import com.u2ware.springfield.config.Springfield;
import com.u2ware.springfield.config.Springfield.Strategy;
@Springfield(
strategy=Strategy.JPA
)
@Entity
public class ValidationBean {
@NotNull @Id
private @Getter @Setter Integer intValue;
private @Getter @Setter String stringValue;
@NotNull @NumberFormat(pattern="0.0000")
private @Getter @Setter Float floatValue;
@NotNull @DateTimeFormat(pattern="yyyy-MM-dd")
@Type(type="org.joda.time.contrib.hibernate.PersistentDateTime")
private @Getter @Setter DateTime dateTimeValue;
@NotNull
private @Getter @Setter EnumValue enumValue;
}
package example.u2ware.springfield.part4.step1;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import com.u2ware.springfield.controller.EntityValidatorImpl;
@Component
public class ValidationBeanValidator extends EntityValidatorImpl<ValidationBean,ValidationBean>{
protected final Log logger = LogFactory.getLog(getClass());
@Override
public void create(ValidationBean target, Errors errors) {
super.create(target, errors);
ValidationUtils.rejectIfEmpty(errors, "stringValue", null, "empty");
ValidationBean bean = (ValidationBean)target;
if("NO".equalsIgnoreCase(bean.getStringValue())){
errors.rejectValue("stringValue" , null, "'NO' equals Ignore Case");
}
}
}
springfield 는 JSR-303 Bean Validation 과 org.springframework.validation.Validator 를 함계 지원한다. CoC 에의해 org.springframework.validation.Validator Bean 이 Context 에 존재 한다면 이를 찾아서 실행한다.