⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 formfieldvalidator.java

📁 欢迎使用 FastJsp 开发框架! 编译说明: * 若要生成Api Javadoc文档
💻 JAVA
字号:
// Copyright 2005-2007 onetsoft.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.onetsoft.fastjsp.valid;

import com.onetsoft.fastjsp.AbstractFormAction;
import com.onetsoft.fastjsp.AbstractPageCycle;
import com.onetsoft.fastjsp.AbstractServicer;
import com.onetsoft.fastjsp.PageCycle;

import java.text.MessageFormat;

/**
 * 表单域验证
 * 用户只需配置方法{@link this# getValidators()},由类{@link AbstractFormAction}执行自动验证
 *
 * @author <a href="mailto:hgw@onetsoft.com">hgw</a>
 */
public class FormFieldValidator extends AbstractFieldValidator {

    private Validator[] a = new Validator[4];
    private String field = null;
    private String displayName = null;

    private int size = 0;

    /**
     * @param field
     * @param displayName 验证域显示名称
     */
    public FormFieldValidator(String field, String displayName) {
        this.field = field;
        this.displayName = displayName;
    }


    public FieldValidator addValidator(Validator validator) {
        if (size >= a.length)
            ensureCapacity();
        a[size++] = validator;
        return this;
    }

    private void ensureCapacity() {
        int n = (int) (a.length * 1.5);
        if (n <= size) {
            n = size + 1;
        }
        Validator[] b = new Validator[n];
        System.arraycopy(a, 0, b, 0, a.length);
        this.a = b;
    }

    public String getField() {
        return field;
    }

    public String getDisplayName() {
        return displayName;
    }

    public Validator[] getValidators() {
        if (a.length != size) {
            Validator[] b = new Validator[size];
            System.arraycopy(a, 0, b, 0, size);
            this.a = b;
        }
        return this.a;
    }

    public void validate(PageCycle cycle) {
        ValidationMessageProvider provider = ((AbstractServicer) (((AbstractPageCycle) cycle).getServicer())).getModule()
                .getValidationMessageProvider();
         String message ;
        for (int i = 0; i < size; i++) {
            message = a[i].validate(cycle.getPage().getData().getString(field), cycle.getPage().getLocale(), provider);
            if (message != null) {
                message = MessageFormat.format(message, new String[]{displayName});
                return;
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -