Author: pyaschenko
Date: 2011-01-13 10:49:52 -0500 (Thu, 13 Jan 2011)
New Revision: 20994
Added:
branches/RF-9797/ui/validator/ui/src/main/resources/META-INF/resources/org.richfaces/richfaces-message.js
Modified:
branches/RF-9797/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ClientOnlyScript.java
branches/RF-9797/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ValidatorScriptBase.java
branches/RF-9797/ui/validator/ui/src/main/resources/META-INF/resources/org.richfaces/richfaces-csv.js
branches/RF-9797/ui/validator/ui/src/main/resources/META-INF/richfaces/org.richfaces/csv.library.properties
branches/RF-9797/ui/validator/ui/src/main/resources/META-INF/richfaces/org.richfaces/message.library.properties
Log:
RF-9797
Modified:
branches/RF-9797/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ClientOnlyScript.java
===================================================================
---
branches/RF-9797/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ClientOnlyScript.java 2011-01-13
15:31:54 UTC (rev 20993)
+++
branches/RF-9797/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ClientOnlyScript.java 2011-01-13
15:49:52 UTC (rev 20994)
@@ -3,6 +3,8 @@
import java.util.Collection;
import java.util.LinkedHashSet;
+import org.ajax4jsf.javascript.JSFunction;
+import org.ajax4jsf.javascript.JSReference;
import org.ajax4jsf.javascript.ScriptWithDependencies;
import org.richfaces.resource.ResourceKey;
@@ -40,7 +42,7 @@
protected Object buildBody() {
StringBuilder body = new StringBuilder();
// Get component value by clientId.
- body.append("var
").append(ClientValidatorRenderer.VALUE_VAR).append("=");
+ /*body.append("var
").append(ClientValidatorRenderer.VALUE_VAR).append("=");
GET_VALUE_FUNCTION.appendScriptToStringBuilder(body);
body.append(EOL);
// Try client-side validation
@@ -59,7 +61,15 @@
// Catch errors
body.append("} catch(e) {\n");
SEND_ERROR_FUNCTION.appendScriptToStringBuilder(body);body.append(EOL);
- body.append("return false;\n}");
+ body.append("return false;\n}");*/
+ JSFunction callValidator = new JSFunction("RichFaces.csv.validate",
+ new JSReference("event"),
+ new JSReference("id"),
+ new JSReference("element"),
+ new JSReference("c"),
+ new JSReference("v"),
+ new JSReference("p"));
+ body.append(callValidator);
return body;
}
Modified:
branches/RF-9797/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ValidatorScriptBase.java
===================================================================
---
branches/RF-9797/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ValidatorScriptBase.java 2011-01-13
15:31:54 UTC (rev 20993)
+++
branches/RF-9797/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ValidatorScriptBase.java 2011-01-13
15:49:52 UTC (rev 20994)
@@ -14,7 +14,7 @@
public static final JSReference EVENT_LITERAL = new JSReference(EVENT);
public static final String DISABLE_AJAX = "disableAjax";
public static final String EOL = ";\n";
- public static final String CLIENT_ID = "clientId";
+ public static final String CLIENT_ID = "id";
public static final JSReference CLIENT_ID_LITERAL = new JSReference(CLIENT_ID);
public static final String SOURCE_ID = "sourceId";
public static final JSReference SOURCE_ID_LITERAL = new JSReference(SOURCE_ID);
@@ -28,7 +28,7 @@
private boolean bodyProcessed = false;
public ValidatorScriptBase() {
- super(CLIENT_ID,ELEMENT,EVENT,DISABLE_AJAX);
+ super(CLIENT_ID,EVENT,ELEMENT);
}
public void appendScript(Appendable target) throws IOException {
Modified:
branches/RF-9797/ui/validator/ui/src/main/resources/META-INF/resources/org.richfaces/richfaces-csv.js
===================================================================
---
branches/RF-9797/ui/validator/ui/src/main/resources/META-INF/resources/org.richfaces/richfaces-csv.js 2011-01-13
15:31:54 UTC (rev 20993)
+++
branches/RF-9797/ui/validator/ui/src/main/resources/META-INF/resources/org.richfaces/richfaces-csv.js 2011-01-13
15:49:52 UTC (rev 20994)
@@ -8,6 +8,33 @@
var RE_MESSAGE_PATTERN = /\{(\d+)\}/g;
+ var __interpolateMessage = function (customMessage, values) {
+ var message = customMessage || "";
+ if (message) {
+ var msgObject =
message.replace(RE_MESSAGE_PATTERN,"\n$1\n").split("\n");
+ var value;
+ for (var i=1; i<msgObject.length; i+=2) {
+ value = values[msgObject[i]];
+ msgObject[i] = typeof value == "undefined" ? "" : value;
+ }
+ message = msgObject.join('');
+ }
+ return message;
+ }
+
+ var __getValue = function(id) {
+ var value;
+ var element = rf.getDomElement(id);
+ if (element.value) {
+ value = element.value;
+ } else {
+ var component = rf.$(element);
+ // TODO: add getValue to baseComponent and change jsdocs
+ value = component && typeof component["getValue"] ==
"function" ? component.getValue() : "";
+ }
+ return value;
+ }
+
$.extend(rf.csv, {
RE_DIGITS: /^-?\d+$/,
RE_FLOAT: /^(-?\d+)?(\.(\d+)?(e[+-]?\d+)?)?$/,
@@ -16,17 +43,8 @@
$.extend(_messages, messagesObject);
},
getMessage: function(customMessage, messageId, values) {
- var message = customMessage || _messages[messageId] || "";
- if (message) {
- var msgObject =
message.replace(RE_MESSAGE_PATTERN,"\n$1\n").split("\n");
- var value;
- for (var i=1; i<msgObject.length; i+=2) {
- value = values[msgObject[i]];
- msgObject[i] = typeof value == "undefined" ? "" : value;
- }
- message = msgObject.join('');
- }
- return {message:message};
+ var message = customMessage ? {detail:customMessage,summary:customMessage} :
_messages[messageId] || {detail:"",summary:""};
+ return
{detail:__interpolateMessage(message.detail,values),summary:__interpolateMessage(message.summary,values)};
},
sendMessage: function (componentId, message) {
rf.Event.fire(window.document, rf.Event.MESSAGE_EVENT_TYPE,
{'sourceId':componentId, 'message':message});
@@ -45,16 +63,8 @@
getValidator: function (name) {
return _validators[name];
},
- validate: function (event, id, converter, validators) {
- var value;
- var element = rf.getDomElement(id);
- if (element.value) {
- value = element.value;
- } else {
- var component = rf.$(element);
- // TODO: add getValue to baseComponent and change jsdocs
- value = component && typeof component["getValue"] ==
"function" ? component.getValue() : "";
- }
+ validate: function (event, id, element, converter, validators, params) {
+ var value = __getValue(element || id);
if (converter) {
try {
converter.options.componentId = id;
@@ -80,9 +90,165 @@
}
return true;
},
+ /*
+ function form_3Atext_3Av(clientId,element,event,disableAjax){var
value=RichFaces.csv.getValue(clientId,element);
+ try {
+ var convertedValue=value;
+ RichFaces.csv.validateLength(convertedValue,{"detail":"{2}: Validation
Error: Specified attribute is not between the expected values of {0} and
{1}.","severity":0,"summary":"{2}: Validation Error:
Specified attribute is not between the expected values of {0} and {1}."}
,{"minimum":1,"maximum":3} );
+ if(!disableAjax){
+ RichFaces.csv.clearMessage(clientId);
+ }
+ return true;
+ } catch(e) {
+ RichFaces.csv.sendMessage(clientId,e);
+ return false;
+ }}
+ */
+ /*
+ * c: {name:, options:}
+ v: [{type:, customMessage:, [validator params]}]
+ function form_3Atext_3Av(id, el, e, c, v, p) {
+ RichFaces.csv.validate(e, id, el, c, v, p);
+ }
+ */
addFormValidators: function (formId, callValidatorFunctions) {
}
});
+})(jQuery, window.RichFaces || (window.RichFaces={}));
+
+/*
+ * Converters implementation
+ */
+(function($, rf) {
+ RichFaces.csv.addConverter({
+ "boolean": function (value, params) {
+ var result; value = $.trim(value).toLowerCase();
+ result = value=='true' ? true : value.length<1 ? null : false;
+
+ return result;
+ },
+ "byte": function (value, params) {
+ var result; value = $.trim(value);
+ if (!rf.csv.RE_DIGITS.test(value) || (result=parseInt(value,10))<-128 ||
result>127) {
+ throw rf.csv.getMessage(params.customMessage, 'BYTE_CONVERTER_BYTE', [value,
0, params.componentId]);
+ }
+ return result;
+ },
+ "number": function (value, params) {
+ var result; value=$.trim(value);
+ if (isNaN(value)) {
+ throw rf.csv.getMessage(params.customMessage, 'NUMBER_CONVERTER_NUMBER',
[value, 0, params.componentId]);
+ } else {
+ result = parseInt(value, 10);
+ }
+ return result;
+ },
+ "short": function (value, params) {
+ var result; value = $.trim(value);
+ if (!rf.csv.RE_DIGITS.test(value) || (result=parseInt(value,10))<-32768 ||
result>32767) {
+ throw rf.csv.getMessage(params.customMessage, 'SHORT_CONVERTER_SHORT',
[value, 0, params.componentId]);
+ }
+ return result;
+ }
+ });
+})(jQuery, window.RichFaces || (window.RichFaces={}));
+
+/*
+ * Validators implementation
+ */
+(function($, rf) {
+ RichFaces.csv.addValidator({
+ "long-range": function (componentId, value, params) {
+ var type = typeof value;
+ if (type != "number") {
+ if (type != "string") {
+ throw rf.csv.getMessage(params.customMessage, 'LONG_RANGE_VALIDATOR_TYPE',
[componentId, ""]);
+ } else {
+ value = $.trim(value);
+ if (!rf.csv.RE_DIGITS.test(value) || (value = parseInt(value, 10))==NaN) {
+ throw rf.csv.getMessage(params.customMessage, 'LONG_RANGE_VALIDATOR_TYPE',
[componentId, ""]);
+ }
+ }
+ }
+
+ var isMinSet = typeof params.minimum == "number";
+ var isMaxSet = typeof params.maximum == "number";
+
+ if (isMaxSet && value > params.maximum) {
+ if (isMinSet) {
+ throw rf.csv.getMessage(params.customMessage,
'LONG_RANGE_VALIDATOR_NOT_IN_RANGE', [params.minimum, params.maximum,
componentId]);
+ } else {
+ throw rf.csv.getMessage(params.customMessage,
'LONG_RANGE_VALIDATOR_MAXIMUM', [params.maximum, componentId]);
+ }
+ }
+ if (isMinSet && value < params.minimum) {
+ if (isMaxSet) {
+ throw rf.csv.getMessage(params.customMessage,
'LONG_RANGE_VALIDATOR_NOT_IN_RANGE', [params.minimum, params.maximum,
componentId]);
+ } else {
+ throw rf.csv.getMessage(params.customMessage,
'LONG_RANGE_VALIDATOR_MINIMUM', [params.minimum, componentId]);
+ }
+ }
+ },
+ "double-range": function (componentId, value, params) {
+ var type = typeof value;
+ if (type != "number") {
+ if (type != "string") {
+ throw rf.csv.getMessage(params.customMessage, 'DOUBLE_RANGE_VALIDATOR_TYPE',
[componentId, ""]);
+ } else {
+ value = $.trim(value);
+ if (!rf.csv.RE_FLOAT.test(value) || (value = parseFloat(value))==NaN) {
+ throw rf.csv.getMessage(params.customMessage,
'DOUBLE_RANGE_VALIDATOR_TYPE', [componentId, ""]);
+ }
+ }
+ }
+
+ var isMinSet = typeof params.minimum == "number";
+ var isMaxSet = typeof params.maximum == "number";
+
+ if (isMaxSet && value > params.maximum) {
+ if (isMinSet) {
+ throw rf.csv.getMessage(params.customMessage,
'DOUBLE_RANGE_VALIDATOR_NOT_IN_RANGE', [params.minimum, params.maximum,
componentId]);
+ } else {
+ throw rf.csv.getMessage(params.customMessage,
'DOUBLE_RANGE_VALIDATOR_MAXIMUM', [params.maximum, componentId]);
+ }
+ }
+ if (isMinSet && value < params.minimum) {
+ if (isMaxSet) {
+ throw rf.csv.getMessage(params.customMessage,
'DOUBLE_RANGE_VALIDATOR_NOT_IN_RANGE', [params.minimum, params.maximum,
componentId]);
+ } else {
+ throw rf.csv.getMessage(params.customMessage,
'DOUBLE_RANGE_VALIDATOR_MINIMUM', [params.minimum, componentId]);
+ }
+ }
+ },
+ "length": function (componentId, value, params) {
+ if (params.maximum && value.length > params.maximum) {
+ throw rf.csv.getMessage(params.customMessage, 'LENGTH_VALIDATOR_MAXIMUM',
[params.maximum, componentId]);
+ }
+ if (params.minimum && value.length < params.minimum) {
+ throw rf.csv.getMessage(params.customMessage, 'LENGTH_VALIDATOR_MINIMUM',
[params.minimum, componentId]);
+ }
+ },
+ "regex": function (componentId, value, params) {
+ if (typeof params.pattern != "string" || params.pattern.length == 0) {
+ throw rf.csv.getMessage(params.customMessage,
'REGEX_VALIDATOR_PATTERN_NOT_SET', []);
+ }
+
+ var re;
+ try {
+ re = new RegExp(params.pattern);
+ } catch (e) {
+ throw rf.csv.getMessage(params.customMessage,
'REGEX_VALIDATOR_MATCH_EXCEPTION', []);
+ }
+ if (!re.test(value)){
+ throw rf.csv.getMessage(params.customMessage, 'REGEX_VALIDATOR_NOT_MATCHED',
[params.pattern]);
+ }
+ },
+ "required": function (componentId, value, params) {
+ if (value.length==0) {
+ throw rf.csv.getMessage(params.customMessage, 'UIINPUT_REQUIRED',
[componentId]);
+ }
+ }
+ });
})(jQuery, window.RichFaces || (window.RichFaces={}));
\ No newline at end of file
Added:
branches/RF-9797/ui/validator/ui/src/main/resources/META-INF/resources/org.richfaces/richfaces-message.js
===================================================================
---
branches/RF-9797/ui/validator/ui/src/main/resources/META-INF/resources/org.richfaces/richfaces-message.js
(rev 0)
+++
branches/RF-9797/ui/validator/ui/src/main/resources/META-INF/resources/org.richfaces/richfaces-message.js 2011-01-13
15:49:52 UTC (rev 20994)
@@ -0,0 +1,77 @@
+(function($, rf) {
+
+ rf.ui = rf.ui || {};
+
+ // Constructor definition
+ rf.ui.Base = function(componentId, options, defaultOptions) {
+ this.namespace = "."+rf.Event.createNamespace(this.name, componentId);
+ // call constructor of parent class
+ $super.constructor.call(this, componentId);
+ this.options = $.extend(this.options, defaultOptions, options);
+ this.attachToDom();
+ this.__bindEventHandlers();
+ };
+
+ // Extend component class and add protected methods from parent class to our container
+ rf.BaseComponent.extend(rf.ui.Base);
+
+ // define super class link
+ var $super = rf.ui.Base.$super;
+
+ $.extend(rf.ui.Base.prototype, {
+ __bindEventHandlers: function () {
+ },
+ destroy: function () {
+ rf.Event.unbindById(this.id, this.namespace);
+ $super.destroy.call(this);
+ }
+ });
+
+})(jQuery, window.RichFaces || (window.RichFaces={}));
+
+(function($, rf) {
+
+ // Constructor definition
+ rf.ui.Message = function(componentId, options) {
+ // call constructor of parent class
+ $super.constructor.call(this, componentId, options, defaultOptions);
+ };
+
+ // Extend component class and add protected methods from parent class to our container
+ rf.ui.Base.extend(rf.ui.Message);
+
+ // define super class link
+ var $super = rf.ui.Message.$super;
+
+ var defaultOptions = {
+
+ };
+
+ var componentHash = {};
+ var componentIndex = 0;
+
+ var onMessage = function (event, element, data) {
+ if (!this.options.forComponentId) {
+ var index = componentHash[data.sourceId];
+ if (typeof index != undefined) {
+ $(rf.getDomElement(this.id+index)).remove();
+ }
+ var content = content = $(rf.getDomElement(this.id));
+
+ componentIndex ++;
+ if (data.message) content.append('<li
id="'+this.id+componentIndex+'">'+data.message.summary+'</li>');
+ componentHash[data.sourceId] = componentIndex;
+
+ } else if (this.options.forComponentId==data.sourceId) {
+ rf.getDomElement(this.id).innerHTML = data.message ?
'<li>'+data.message.summary+'</li>' : '';
+ }
+ }
+
+ $.extend(rf.ui.Message.prototype, {
+ name: "Message",
+ __bindEventHandlers: function () {
+ rf.Event.bind(window.document, rf.Event.MESSAGE_EVENT_TYPE+this.namespace, onMessage,
this);
+ }
+ });
+
+})(jQuery, window.RichFaces || (window.RichFaces={}));
\ No newline at end of file
Modified:
branches/RF-9797/ui/validator/ui/src/main/resources/META-INF/richfaces/org.richfaces/csv.library.properties
===================================================================
---
branches/RF-9797/ui/validator/ui/src/main/resources/META-INF/richfaces/org.richfaces/csv.library.properties 2011-01-13
15:31:54 UTC (rev 20993)
+++
branches/RF-9797/ui/validator/ui/src/main/resources/META-INF/richfaces/org.richfaces/csv.library.properties 2011-01-13
15:49:52 UTC (rev 20994)
@@ -1 +1 @@
-resources=jquery.js, richfaces.js, richfaces-event.js, org.richfaces:csv.js
\ No newline at end of file
+resources=jquery.js, richfaces.js, richfaces-event.js, org.richfaces:richfaces-csv.js
\ No newline at end of file
Modified:
branches/RF-9797/ui/validator/ui/src/main/resources/META-INF/richfaces/org.richfaces/message.library.properties
===================================================================
---
branches/RF-9797/ui/validator/ui/src/main/resources/META-INF/richfaces/org.richfaces/message.library.properties 2011-01-13
15:31:54 UTC (rev 20993)
+++
branches/RF-9797/ui/validator/ui/src/main/resources/META-INF/richfaces/org.richfaces/message.library.properties 2011-01-13
15:49:52 UTC (rev 20994)
@@ -1 +1 @@
-resources=jquery.js, richfaces.js, richfaces-event.js, richfaces-base-component.js,
org.richfaces:message.js
\ No newline at end of file
+resources=jquery.js, richfaces.js, richfaces-event.js, richfaces-base-component.js,
org.richfaces:richfaces-message.js
\ No newline at end of file