// Handle protocol parametrisation.
if(!isWidgetSupported(protocol)) {
return;
}
ValidateAttributes validateAttributes = (ValidateAttributes) attributes;
// Rendering is different, if validate element is used in simple
// validator or in multiple validator.
if (!validateAttributes.isMultiple()) {
// Close invisible <span> element containg content of
// the validation messages
DOMOutputBuffer currentBuffer = getCurrentBuffer(protocol);
currentBuffer.closeElement("span");
require(WIDGET_VALIDATOR, protocol, attributes);
// Gather all required validate attributes.
String messageArea = validateAttributes.getMessageArea();
String messagePopup = validateAttributes.getMessagePopup();
// Render JavaScript
ScriptAttributes scriptAttributes = new ScriptAttributes();
scriptAttributes.setLanguage("JavaScript");
scriptAttributes.setType("text/javascript");
openScriptElement(scriptAttributes, currentBuffer);
StringWriter scriptWriter = new StringWriter();
try {
scriptWriter.write("Widget.register(" + createJavaScriptString(validateAttributes.getId()) + ",");
scriptWriter.write("new Widget.SimpleValidator({");
scriptWriter.write("inputId:" + createJavaScriptString(validateAttributes.getInputElementId()));
// Render input format
String inputFormat = validateAttributes.getInputFormat();
if (inputFormat != null) {
scriptWriter.write(", inputFormat:" + createJavaScriptString(inputFormat));
}
// Render URL of the validator
String src = validateAttributes.getSrc();
if (src != null) {
scriptWriter.write(", url:" + createJavaScriptString(src));
}
// Render styles for input element
scriptWriter.write(", invalidInputStyle: " + validateAttributes.getInvalidStyle());
// Render message-area stuff.
if (validateAttributes.containsMessageValidationErrorAction()) {
// Render message for 'empty-input' validation error.
String emptyMessageElementId = validateAttributes.getEmptyMessageElementId();
if (emptyMessageElementId != null) {
scriptWriter.write(", emptyMessageId:" + createJavaScriptString(emptyMessageElementId));
} else {
String emptyMessage = messageLocalizer.format("input-required");
scriptWriter.write(", emptyMessage:" + createJavaScriptString(emptyMessage + "."));
}
// Render message for 'invalid-input' validation error.
String invalidMessageElementId = validateAttributes
.getInvalidMessageElementId();
if (invalidMessageElementId != null) {
scriptWriter.write(", invalidMessageId:" + createJavaScriptString(invalidMessageElementId));
} else {
String invalidMessage = messageLocalizer.format("input-invalid");
scriptWriter.write(", invalidMessage:" + createJavaScriptString(invalidMessage + "."));
}
// If there's no messageArea and no messagePopup are
// specified, display validation failures in default
// JavaScript alert.
// Otherwise, display message in message-area (if
// specified), and display popup instance (if specified).
// Note, that message-area can refer to content inside
// the popup instance. The effect would be that before
// popup is shows, its content is filled with specified
// message.
if ((messageArea == null) && (messagePopup == null)) {
scriptWriter.write(", displayAlerts: true");
} else {
if (messageArea != null) {
scriptWriter.write(", messageAreaId: " + createJavaScriptString(messageArea));
}
if (messagePopup != null) {
scriptWriter.write(", popupId: " + createJavaScriptString(messagePopup));
}
}
}
// Render autoFocus attribute
if (validateAttributes.containsFocusValidationErrorAction()) {
scriptWriter.write(", autoFocus: true");
}
// Close all parentheses.
scriptWriter.write("}));");
// Register the same instance of widget under the ID of the
// input field, so that it can be accessed from multiple validator
// on client-side.
// TODO: This is only temporary solution. Ideal solution would be
// to pass list of simple validators instances directly in the
// constructor of multiple validator.
scriptWriter.write("Widget.register("
+ createJavaScriptString(validateAttributes.getInputElementId())
+ ", Widget.getInstance(" + createJavaScriptString(validateAttributes.getId()) + "));");
// temporary workaround until new device policy is introduced (content for onblur or onmouse event will be added by javascript method)
scriptWriter.write("Widget.addStartupItem(function(){Widget.getInstance(" + createJavaScriptString(validateAttributes.getId()) + ").setInputEvents()})");
// Flush JavaScript to the page.
writeJavaScript(currentBuffer, scriptWriter.toString());
} catch (IOException e) {
throw new ProtocolException(e);
}
// Close script element.
closeScriptElement(currentBuffer);
// Register this validator widget in the possible enclosing wizard
// widget.
WidgetDefaultModule widgetModule =
(WidgetDefaultModule) protocol.getWidgetModule();
WizardDefaultRenderer wizardRenderer =
(WizardDefaultRenderer) widgetModule.getWizardRenderer();
if (wizardRenderer != null) {
wizardRenderer.renderRegisterSimpleValidator(protocol, validateAttributes.getId());
}
}
}