parent
8fbfb51c0a
commit
3b4509d0a7
@ -0,0 +1,68 @@
|
||||
package info.bukova.isspst.ui;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
import org.apache.commons.beanutils.BeanUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.zkoss.bind.SimpleForm;
|
||||
|
||||
|
||||
/**
|
||||
* Třída middle objektu pro validaci formuláře. Umožňuje uložit formulář do datového objektu bez volání validace.
|
||||
* Do definice formuláře v ZUL souboru je potřeba uvést: {@code @init(vm.dataForm) }.
|
||||
*
|
||||
* @author pepa
|
||||
*
|
||||
* @param <T> Třída datového objektu
|
||||
*/
|
||||
public class BindingForm<T> extends SimpleForm {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = -9052402254058071780L;
|
||||
private T dataBean;
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(BindingForm.class);
|
||||
|
||||
|
||||
/**
|
||||
* Uloží formulář do předaného objektu.
|
||||
*
|
||||
* @param object Datový objekt
|
||||
*/
|
||||
public void bindTo(T object) {
|
||||
for (String key : getFieldNames()) {
|
||||
try {
|
||||
BeanUtils.setProperty(object, key, getField(key));
|
||||
} catch (IllegalAccessException e) {
|
||||
logger.warn("Cannot bind value", e);
|
||||
} catch (InvocationTargetException e) {
|
||||
logger.warn("Cannot bind value", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Uloží formulář do členského datového objektu.
|
||||
*/
|
||||
public void bind() {
|
||||
bindTo(dataBean);
|
||||
}
|
||||
|
||||
public T getDataBean() {
|
||||
return dataBean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Nastaví datový objekt této instance {@link BindingForm}
|
||||
*
|
||||
* @param dataBean Datový objekt
|
||||
*/
|
||||
public void setDataBean(T dataBean) {
|
||||
this.dataBean = dataBean;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package info.bukova.isspst.validators;
|
||||
|
||||
import org.zkoss.bind.ValidationContext;
|
||||
|
||||
public class AddressValidator extends BaseValidator {
|
||||
|
||||
private static final String EMAIL_PATTERN =
|
||||
"^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
|
||||
+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
|
||||
private static final String URL_PATTERN = "^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]";
|
||||
|
||||
@Override
|
||||
public void validate(ValidationContext ctx) {
|
||||
if (!validateIsNotNullOrNotEmpty(ctx, "company", "CompanyIsEmpty", "company")) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!validateIsNotNullOrNotEmpty(ctx, "city", "CityIsEmpty", "city")) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!validateRegExp(ctx, "email", "EmailFormatIncorrect", "email", EMAIL_PATTERN)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!validateRegExp(ctx, "web", "WebUrlFormatIncorrect", "web", URL_PATTERN)) {
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue