From a056c620d3d1d55947753998c87c86de45283880 Mon Sep 17 00:00:00 2001 From: Josef Rokos Date: Thu, 22 May 2014 10:22:20 +0200 Subject: [PATCH] =?UTF-8?q?Zobrazov=C3=A1n=C3=AD=20chyb=20validace=20ZK=20?= =?UTF-8?q?frameworkem?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../info/bukova/isspst/ui/FormViewModel.java | 8 +++ .../bukova/isspst/ui/ServiceConstraint.java | 54 +++++++++++++++++++ src/main/webapp/admin/addressbook/address.zul | 17 +++--- src/main/webapp/buildings/buildingForm.zul | 6 +-- 4 files changed, 69 insertions(+), 16 deletions(-) create mode 100644 src/main/java/info/bukova/isspst/ui/ServiceConstraint.java diff --git a/src/main/java/info/bukova/isspst/ui/FormViewModel.java b/src/main/java/info/bukova/isspst/ui/FormViewModel.java index a5dab5a5..f0dc3b4e 100644 --- a/src/main/java/info/bukova/isspst/ui/FormViewModel.java +++ b/src/main/java/info/bukova/isspst/ui/FormViewModel.java @@ -21,11 +21,15 @@ public class FormViewModel { private Map errMessages; private Service service; private boolean newRec; + private ServiceConstraint constraint; @Init public void init(@ExecutionArgParam("selected") T selected, @ExecutionArgParam("service") Service service) { this.dataBean = selected; this.service = service; + constraint = new ServiceConstraint(); + constraint.setDataBean(selected); + constraint.setService(service); if (selected.getId() == 0 && selected.getCreated() == null) { newRec = true; } else { @@ -33,6 +37,10 @@ public class FormViewModel { } } + public ServiceConstraint getConstriant() { + return constraint; + } + public T getDataBean() { return dataBean; } diff --git a/src/main/java/info/bukova/isspst/ui/ServiceConstraint.java b/src/main/java/info/bukova/isspst/ui/ServiceConstraint.java new file mode 100644 index 00000000..dd0e4283 --- /dev/null +++ b/src/main/java/info/bukova/isspst/ui/ServiceConstraint.java @@ -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 implements Constraint { + + private Service 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 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 service) { + this.service = service; + } + + public void setDataBean(T dataBean) { + this.dataBean = dataBean; + } + +} diff --git a/src/main/webapp/admin/addressbook/address.zul b/src/main/webapp/admin/addressbook/address.zul index 2d54e43c..9fe94a87 100644 --- a/src/main/webapp/admin/addressbook/address.zul +++ b/src/main/webapp/admin/addressbook/address.zul @@ -12,12 +12,10 @@ - -