?? formfieldvalidator.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 + -