// clear messages
this._messages.clear();
// init expression engine
RhinoExpressionEngine engine = (RhinoExpressionEngine) ExpressionEngineFactory.getEngine(ExpressionEngineFactory.ENGINE_TMLSCRIPT);
if (engine == null) {
TMLContext.getThreadMainContext().addwarning("Formvalidation cannot be processed. Error initializing tmlscript-engine.", false);
return false;
}
// validate each field
Iterator fieldIt = fields.values().iterator();
boolean formIsValid = true;
while (fieldIt.hasNext()) {
TMLFormField field = (TMLFormField) fieldIt.next();
// check if we have a registration for this field
// we can only validate tml:input fields
if (_formInfo.containsFieldRegistration(field.getName())) {
if (validateField(field, formContext, validationContext, engine) == false) {
formIsValid = false;
}
}
}
// validate global validations (tml:validate tag)
Iterator globalValidations = _formInfo.getFormValidations().keySet().iterator();
while (globalValidations.hasNext()) {
String expression = (String) globalValidations.next();
FormInfo.FormValidation formValidation = (FormInfo.FormValidation) _formInfo.getFormValidations().get(expression);
// check if formvalidation should be processed this time
// all dependent fields (from attrib ifnoerror of tml:validate) are validated successfully
boolean executeYet = true;
Iterator ifnoerrorFields = formValidation.getIfnoerror().iterator();
while (ifnoerrorFields.hasNext()) {
String fieldname = (String) ifnoerrorFields.next();
if (_messages.containsKey(fieldname)) {
executeYet = false;
break;
}
}
if (executeYet) {
ExpressionResult result = engine.evaluateExpression(expression, validationContext, ExpressionEngine.TYPE_EXPRESSION, buildValidationExpressionParams(null));
if (result.isError()) {
formIsValid = false;
String errorMsg = "Validation-Expression could not be processed. Warning: " + result.getException().getMessage() + " - expression was: " + expression;
if (result.getException() != null) {
// See if there is a TMLFormValidationException "somewhere down there". If so we take it as negative validation result
Throwable cause = result.getException();
while (!(cause instanceof TMLFormValidationException) && cause.getCause() != null && cause.getCause() != cause) {
cause = cause.getCause();
}
if (cause instanceof TMLFormValidationException) {
errorMsg = cause.getMessage();
}
else {
TMLContext.getThreadMainContext().addwarning(errorMsg, false);
TMLContext.getThreadMainContext().getlog().error("Error running validation expression", result.getException());
}
}
log.debug(errorMsg);
_globalMessages.put(expression, errorMsg);
// clear given dependent fields
clearFields(formValidation.getCleariferror());
}
else if (result.isFalse()) {
formIsValid = false;
// resolve scriptlets in message
Map params = new HashMap();
params.put(RhinoExpressionEngine.PARAM_LEVEL, RhinoExpressionEngine.LEVEL_SCRIPTLETS);
String message = engine.resolveScriptlets(formValidation.getMessage(), validationContext, params);
_globalMessages.put(expression, message);
log.debug("Validation result for expression '" + expression + "' is '" + result.isTrue() + "'.");
// clear given dependent fields
clearFields(formValidation.getCleariferror());
} else if (result.isTrue()) {