// 20030918 DDJ: Added method public static boolean basicValidateStringArray( ActionErrors actionErrors, String field, String[] value, Number entityField, IGTEntity entity) throws GTClientException { try { IGTFieldMetaInfo fmi = entity.getFieldMetaInfo(entityField); if (fmi == null) { throw new java.lang.NullPointerException( "No fieldMetaInfo for field:" + entityField + "(" + field + ") of entity " + entity); } boolean newEntity = entity.isNewEntity(); if (!fmi.isDisplayable(newEntity) || !fmi.isEditable(newEntity)) { return true; } if (fmi.isMandatory(newEntity)) { if (value == null || value.length == 0) { // actionErrors.add(field,new ActionError(entity.getType() + ".error." + field + // ".required")); addFieldError(actionErrors, field, entity.getType(), REQUIRED, null); return false; } } // validate based on value class String valueClass = fmi.getValueClass(); for (int i = 0; i < value.length; i++) { try { StaticUtils.convert(value[i], valueClass, true, false); } catch (UnsupportedOperationException valueClassUnsupported) { // swallow, ignore testing unsupported value class } catch (Throwable t) { addFieldError(actionErrors, field, entity.getType(), INVALID, null); return false; } } return true; } catch (Throwable t) { throw new GTClientException("Error validating field " + entityField, t); } }
/** * Performs rudimentary string field validation checking for the presence of data in fields that * are mandatory and the maximum length of the field. */ public static boolean basicValidateString( ActionErrors actionErrors, String field, String value, Number entityField, IGTEntity entity) throws GTClientException { try { IGTFieldMetaInfo fmi = entity.getFieldMetaInfo(entityField); boolean newEntity = entity.isNewEntity(); if (fmi == null) { throw new java.lang.NullPointerException( "No fieldMetaInfo for field:" + entityField + "(" + field + ") of entity " + entity); } if ((!fmi.isDisplayable(newEntity)) || (!fmi.isEditable(newEntity))) { return true; } if (fmi.isMandatory(newEntity)) { if ((value == null) || (value.equals(""))) { // actionErrors.add(field,new ActionError(entity.getType() + ".error." + field + // ".required")); addFieldError(actionErrors, field, entity.getType(), REQUIRED, null); // 20021220AH return false; } } if (fmi.getConstraintType() == IConstraint.TYPE_TEXT) { ITextConstraint textConstraint = (ITextConstraint) fmi.getConstraint(); /* 20030918 DDJ: Fixed checking of minLength when maxLength = -1 if(textConstraint.getMaxLength() > 0) { int valueLength = value==null ? 0 : value.length(); if( (valueLength > textConstraint.getMaxLength()) || (valueLength < textConstraint.getMinLength()) ) */ int valueLength = (value == null) ? 0 : value.length(); if ((textConstraint.getMaxLength() > 0 && valueLength > textConstraint.getMaxLength()) || (valueLength < textConstraint.getMinLength())) { // actionErrors.add(field,new ActionError(entity.getType() + ".error." + field + // ".invalid")); addFieldError(actionErrors, field, entity.getType(), INVALID, null); // 20021220AH return false; } } boolean valueEmpty = ((value == null) || ("".equals(value))); Number convertedValue = null; if ("java.lang.Integer".equals(fmi.getValueClass()) && !valueEmpty) { try { convertedValue = new Integer(value); } catch (Exception e) { // actionErrors.add(field,new ActionError(entity.getType() + ".error." + field + // ".invalid")); addFieldError(actionErrors, field, entity.getType(), INVALID, null); // 20021220AH return false; } } else if ("java.lang.Short".equals(fmi.getValueClass()) && !valueEmpty) { try { convertedValue = new Short(value); } catch (Exception e) { // actionErrors.add(field,new ActionError(entity.getType() + ".error." + field + // ".invalid")); addFieldError(actionErrors, field, entity.getType(), INVALID, null); // 20021220AH return false; } } else if ("java.lang.Long".equals(fmi.getValueClass()) && !valueEmpty) { try { convertedValue = new Long(value); } catch (Exception e) { // actionErrors.add(field,new ActionError(entity.getType() + ".error." + field + // ".invalid")); addFieldError(actionErrors, field, entity.getType(), INVALID, null); // 20021220AH return false; } } if (convertedValue != null && fmi.getConstraintType() == IConstraint.TYPE_RANGE) { // This is to apply range constraint try { IRangeConstraint rangeConstraint = (IRangeConstraint) fmi.getConstraint(); Number min = rangeConstraint.getMin(0); Number max = rangeConstraint.getMax(0); if (min == null) throw new NullPointerException("min is null"); if (max == null) throw new NullPointerException("min is null"); if ((convertedValue.doubleValue() < min.doubleValue()) || (convertedValue.doubleValue() > max.doubleValue())) throw new Exception("Value out of range, constraint min=" + min + ", max=" + max); } catch (Exception ex) { addFieldError(actionErrors, field, entity.getType(), INVALID, null); return false; } } return true; } catch (Throwable t) { throw new GTClientException("Error validating field " + entityField, t); } }