Java Code Examples for org.javalite.activejdbc.Model#addValidator()
The following examples show how to use
org.javalite.activejdbc.Model#addValidator() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AttributeLengthValidator.java From javalite with Apache License 2.0 | 6 votes |
public void validate(Model m) { Object value = m.get(this.attribute); if(allowBlank && (null == value || "".equals(value))) { return; } if(null == value) { m.addValidator(this, this.attribute); return; } if(!(value instanceof String)) { throw new IllegalArgumentException("Attribute must be a String"); } else { if(!this.lengthOption.validate((String)((String)m.get(this.attribute)))) { //somewhat confusingly this adds an error for a validator. m.addValidator(this, this.attribute); } } }
Example 2
Source File: RegexpValidator.java From javalite with Apache License 2.0 | 5 votes |
@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); } }
Example 3
Source File: TimestampValidator.java From javalite with Apache License 2.0 | 5 votes |
@Override public void validate(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); } } }
Example 4
Source File: NumericValidator.java From javalite with Apache License 2.0 | 5 votes |
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; } }
Example 5
Source File: NumericValidator.java From javalite with Apache License 2.0 | 5 votes |
private void validateIntegerOnly(Object value, Model m){ try{ Integer.valueOf(value.toString()); } catch(NumberFormatException e) { m.addValidator(this, attribute); } }
Example 6
Source File: UniquenessValidator.java From javalite with Apache License 2.0 | 5 votes |
@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); } }
Example 7
Source File: DateValidator.java From javalite with Apache License 2.0 | 5 votes |
@Override public void validate(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); } } }
Example 8
Source File: CustomValidatorTest.java From javalite with Apache License 2.0 | 5 votes |
@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); }
Example 9
Source File: AttributePresenceValidator.java From javalite with Apache License 2.0 | 4 votes |
@Override public void validate(Model m) { if (blank(m.get(attribute))) { m.addValidator(this, attribute); } }
Example 10
Source File: NumericValidator.java From javalite with Apache License 2.0 | 4 votes |
@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); } }
Example 11
Source File: NumericValidator.java From javalite with Apache License 2.0 | 4 votes |
private void validateMin(Double value, Model m){ if(value <= min){ m.addValidator(this, attribute); } }
Example 12
Source File: NumericValidator.java From javalite with Apache License 2.0 | 4 votes |
private void validateMax(Double value, Model m){ if(value >= max){ m.addValidator(this, attribute); } }