本站首页    管理页面    写新日志    退出


«August 2025»
12
3456789
10111213141516
17181920212223
24252627282930
31


公告
 本博客在此声明所有文章均为转摘,只做资料收集使用。

我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:
日志总数:1304
评论数量:2242
留言数量:5
访问次数:7589966
建立时间:2006年5月29日




[Tapestry]Tapestry数据校验-修改Required Validator
软件技术

lhwork 发表于 2006/7/11 17:52:30

Required的修改遇到麻烦事,因为BaseValidator有一个方法isRequired,这样增加一个属性required的getter和 setter,page文件中设置required=searchId时,总是去匹配isRequired方法,这样出现字符串转换为布尔型错误。 为了对其它校验器不产生影响,我增加了一个校验器Required2,配置时required2=searchId。 同时,required类型的校验器本来不需要参数,所以现在参数值就是提交按钮的ID,因而可以使用BaseValidator的submitName属性。代码如下: public class Required2 extends BaseValidator {     public String getRequired2() {         return this.getSubmitName();     }     public void setRequired2(String required) {         this.setSubmitName(required);     }     public Required2() {     }     public Required2(String initializer) {         super(initializer);     }     public boolean getAcceptsNull() {         return true;     }     public void validate(IFormComponent field, ValidationMessages messages,             Object object) throws ValidatorException {         if (!this.isSkipValid(field)) {             if ((object == null)                     || (String.class.isInstance(object) && (((String) object)                             .length() == 0))                     || (Collection.class.isInstance(object) && ((Collection) object)                             .isEmpty())) {                 String message = buildMessage(messages, field);                 throw new ValidatorException(message,                         ValidationConstraint.REQUIRED);             }         }     }     private String buildMessage(ValidationMessages messages,             IFormComponent field) {         return messages.formatValidationMessage(getMessage(),                 ValidationStrings.REQUIRED_FIELD, new Object[] { field                         .getDisplayName() });     }     public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,             FormComponentContributorContext context, IFormComponent field) {         context                 .includeClasspathScript("/com/fitechlabs/xtrade/web/tapestry/validator/RequiredValidator.js");         if (this.isFormValid()) {             context.addSubmitHandler(Script.getFormRequiredScript(field,                     TapestryUtils.enquote(buildMessage(context, field))));         } else {             this.addScriptContent(cycle, field, Script.getSubmitRequiredScript(                     this.getSubmitName(), field, TapestryUtils                             .enquote(buildMessage(context, field))));         }     } } Required2.js: TapestryEx.requiredValid=function(event,formId,fieldId,message){       var field = document.getElementById(formId).elements[fieldId];     if (field.value.length < 1)         event.invalid_field(field, message) } Script.java: public static String getFormRequiredScript(IFormComponent field,             String message) {         StringBuffer buffer = new StringBuffer(                 "function(event) { Tapestry.require_field(event, '");         buffer.append(field.getClientId());         buffer.append("', ");         buffer.append(message);         buffer.append("); }");         return buffer.toString();     }     public static String getSubmitRequiredScript(String submitName,             IFormComponent field, String message) {         StringBuffer buffer = new StringBuffer();         buffer.append("TapestryEx.onclick(");         buffer.append("'" + submitName + "',");         buffer.append("function(event){TapestryEx.requiredValid(event,");         buffer.append("'" + field.getForm().getId() + "',");         buffer.append("'" + field.getClientId() + "',");         buffer.append(message + ");});");         return buffer.toString();     } 需要修改tapestry-4.0.jar/META-INF/tapestry.form.validator.xml的“<contribution configuration-id="Validators">”部分,其中增加 <validator name="required2" class="com.yourcompany.validator.Required2"/>


阅读全文(3670) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.061 second(s), page refreshed 144766745 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号