Java Code Examples for org.javalite.activejdbc.Model#get()

The following examples show how to use org.javalite.activejdbc.Model#get() . 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 vote down vote up
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 vote down vote up
@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 vote down vote up
@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: DateValidator.java    From javalite with Apache License 2.0 5 votes vote down vote up
@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 5
Source File: NumericValidator.java    From javalite with Apache License 2.0 4 votes vote down vote up
@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);
    }
}