本文整理汇总了Java中org.javalite.activejdbc.Model.addValidator方法的典型用法代码示例。如果您正苦于以下问题:Java Model.addValidator方法的具体用法?Java Model.addValidator怎么用?Java Model.addValidator使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.javalite.activejdbc.Model
的用法示例。
在下文中一共展示了Model.addValidator方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: validate
import org.javalite.activejdbc.Model; //导入方法依赖的package包/类
@Override
public void validate(Model m) {
if(m.get(attribute) == null){
m.addValidator(this, attribute);
return;
}
Object value = m.get(attribute);
if (!(value instanceof String)) {
throw new IllegalArgumentException("attribute " + attribute + " is not String");
}
Matcher matcher = pattern.matcher((String) value);
if(!matcher.matches()){
m.addValidator(this, attribute);
}
}
示例2: convert
import org.javalite.activejdbc.Model; //导入方法依赖的package包/类
@Override
public void convert(Model m) {
Object val = m.get(attributeName);
if (!(val instanceof java.util.Date) && !blank(val)) {
try {
long time = df.parse(val.toString()).getTime();
java.sql.Date d = new java.sql.Date(time);
m.set(attributeName, d);
} catch (ParseException e) {
m.addValidator(this, attributeName);
}
}
}
示例3: validate
import org.javalite.activejdbc.Model; //导入方法依赖的package包/类
@Override
public void validate(Model m) {
Object value = m.get(attribute);
if (!(value instanceof String)) {
throw new IllegalArgumentException("Attribute must be a String");
}
if (!lengthOption.validate((String) (m.get(attribute)))) {
m.addValidator(this, attribute);
}
}
示例4: convert
import org.javalite.activejdbc.Model; //导入方法依赖的package包/类
@Override
public void convert(Model m) {
Object val = m.get(attributeName);
if (!(val instanceof Timestamp) && !blank(val)) {
try {
long time = df.parse(val.toString()).getTime();
Timestamp t = new Timestamp(time);
m.set(attributeName, t);
} catch(ParseException e) {
m.addValidator(this, attributeName);
}
}
}
示例5: validate
import org.javalite.activejdbc.Model; //导入方法依赖的package包/类
@Override
public void validate(Model m) {
if (blank(m.get(attribute))) {
//TODO: use resource bundles for messages
m.addValidator(this, attribute);
}
}
示例6: present
import org.javalite.activejdbc.Model; //导入方法依赖的package包/类
private boolean present(Object value, Model m){
if(allowNull){
return true;
}
if(value == null){
setMessage("value is missing");
m.addValidator(this, attribute);
return false;
}else{
return true;
}
}
示例7: validateIntegerOnly
import org.javalite.activejdbc.Model; //导入方法依赖的package包/类
private void validateIntegerOnly(Object value, Model m){
try{
Integer.valueOf(value.toString());
} catch(NumberFormatException e) {
m.addValidator(this, attribute);
}
}
示例8: validate
import org.javalite.activejdbc.Model; //导入方法依赖的package包/类
@Override
public void validate(Model model) {
MetaModel metaModel = metaModelOf(model.getClass());
if (new DB(metaModel.getDbName()).count(metaModel.getTableName(), attribute + " = ?", model.get(attribute)) > 0) {
model.addValidator(this, attribute);
}
}
示例9: shouldRegisterCustomValidator
import org.javalite.activejdbc.Model; //导入方法依赖的package包/类
@Test
public void shouldRegisterCustomValidator() {
class FutureBirthValidator extends ValidatorAdapter {
FutureBirthValidator(){
setMessage("invalid.dob.message");
}
@Override
public void validate(Model m) {
Date dob = m.getDate("dob");
Date now = new java.sql.Date(System.currentTimeMillis());
if(dob.after(now)){
m.addValidator(this, "invalid.dob");//add validator to errors with a key
}
}
}
FutureBirthValidator validator = new FutureBirthValidator();
Person.addValidator(validator);
Person p = new Person();
GregorianCalendar future = new GregorianCalendar();
future.set(Calendar.YEAR, 3000);//will people still be using Java then... or computers? :)
p.set("dob", new Date(future.getTimeInMillis()));
p.validate();
a(p.errors().size()).shouldBeEqual(3);
a(p.errors().get("invalid.dob")).shouldBeEqual("date of birth cannot be in future");
//this is so that other tests succeed
Person.removeValidator(validator);
}
示例10: validate
import org.javalite.activejdbc.Model; //导入方法依赖的package包/类
@Override
public void validate(Model m) {
Object value = m.get(attribute);
if(!present(value, m)){
return;
}
// validators should not also do conversion
if(convertNullIfEmpty && "".equals(value)){
m.set(attribute, null);
value = null;
}
if(value == null && allowNull){
return;
}
//this is to check just numericality
if(!(value instanceof Number)) {
if (value != null) {
ParsePosition pp = new ParsePosition(0);
String input = value.toString();
// toString() is not Locale dependant...
// ... but NumberFormat is. For Polish locale where decimal separator is "," instead of ".". Might fail some tests...
NumberFormat nf = NumberFormat.getInstance();
nf.setParseIntegerOnly(onlyInteger);
nf.parse(input, pp);
if (pp.getIndex() != (input.length())) {
m.addValidator(this, attribute);
}
} else {
m.addValidator(this, attribute);
}
}
if(min != null){
validateMin(Convert.toDouble(value), m);
}
if(max != null){
validateMax(Convert.toDouble(value), m);
}
if(onlyInteger){
validateIntegerOnly(value, m);
}
}
示例11: validateMin
import org.javalite.activejdbc.Model; //导入方法依赖的package包/类
private void validateMin(Double value, Model m){
if(value <= min){
m.addValidator(this, attribute);
}
}
示例12: validateMax
import org.javalite.activejdbc.Model; //导入方法依赖的package包/类
private void validateMax(Double value, Model m){
if(value >= max){
m.addValidator(this, attribute);
}
}