Tapestry数据校验-修改Required Validator

来源:互联网 发布:Tomcat集群 数据库 编辑:程序博客网 时间:2024/06/13 14:28

Tapestry数据校验-修改Required Validator 

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"/>

原创粉丝点击