Zobrazování chyb validace ZK frameworkem

This commit is contained in:
2014-05-22 10:22:20 +02:00
parent 95964aaeeb
commit a056c620d3
4 changed files with 69 additions and 16 deletions
@@ -21,11 +21,15 @@ public class FormViewModel<T extends DataModel> {
private Map<String, String> errMessages;
private Service<T> service;
private boolean newRec;
private ServiceConstraint<T> constraint;
@Init
public void init(@ExecutionArgParam("selected") T selected, @ExecutionArgParam("service") Service<T> service) {
this.dataBean = selected;
this.service = service;
constraint = new ServiceConstraint<T>();
constraint.setDataBean(selected);
constraint.setService(service);
if (selected.getId() == 0 && selected.getCreated() == null) {
newRec = true;
} else {
@@ -33,6 +37,10 @@ public class FormViewModel<T extends DataModel> {
}
}
public ServiceConstraint<T> getConstriant() {
return constraint;
}
public T getDataBean() {
return dataBean;
}
@@ -0,0 +1,54 @@
package info.bukova.isspst.ui;
import info.bukova.isspst.data.DataModel;
import info.bukova.isspst.services.Service;
import info.bukova.isspst.services.ValidationException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zul.Constraint;
public class ServiceConstraint<T extends DataModel> implements Constraint {
private Service<T> service;
private T dataBean;
@Override
public void validate(Component component, Object value)
throws WrongValueException {
String id = component.getId();
if (id == null || id.isEmpty()) {
return;
}
try {
BeanUtils.setProperty(dataBean, id, value);
service.validate(dataBean);
} catch (ValidationException e) {
Map<String, String> errMessages = e.getMessages();
if (errMessages != null && errMessages.get(id) != null && !errMessages.get(id).isEmpty()) {
WrongValueException ex = new WrongValueException(component, errMessages.get(id));
throw ex;
}
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
}
public void setService(Service<T> service) {
this.service = service;
}
public void setDataBean(T dataBean) {
this.dataBean = dataBean;
}
}