Zobrazování chyb validace ZK frameworkem

multitenant
Josef Rokos 11 years ago
parent 95964aaeeb
commit a056c620d3

@ -21,11 +21,15 @@ public class FormViewModel<T extends DataModel> {
private Map<String, String> errMessages; private Map<String, String> errMessages;
private Service<T> service; private Service<T> service;
private boolean newRec; private boolean newRec;
private ServiceConstraint<T> constraint;
@Init @Init
public void init(@ExecutionArgParam("selected") T selected, @ExecutionArgParam("service") Service<T> service) { public void init(@ExecutionArgParam("selected") T selected, @ExecutionArgParam("service") Service<T> service) {
this.dataBean = selected; this.dataBean = selected;
this.service = service; this.service = service;
constraint = new ServiceConstraint<T>();
constraint.setDataBean(selected);
constraint.setService(service);
if (selected.getId() == 0 && selected.getCreated() == null) { if (selected.getId() == 0 && selected.getCreated() == null) {
newRec = true; newRec = true;
} else { } else {
@ -33,6 +37,10 @@ public class FormViewModel<T extends DataModel> {
} }
} }
public ServiceConstraint<T> getConstriant() {
return constraint;
}
public T getDataBean() { public T getDataBean() {
return dataBean; 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;
}
}

@ -12,12 +12,10 @@
<column label="" hflex="min"/> <column label="" hflex="min"/>
<column label="" hflex="min"/> <column label="" hflex="min"/>
<column label=""/> <column label=""/>
<column label=""/>
</columns> </columns>
<rows> <rows>
<row> <row>
<label value="Firma" /> <textbox value="@bind(vm.dataBean.company)" instant="true"/> <label value="Firma" /> <textbox id="company" constraint="@load(vm.constriant)" value="@bind(vm.dataBean.company)" instant="true" width="320px"/>
<label visible="true" value="@load(vm.errMessages['company'])" style="color:red"/>
<button image="/img/search.png" label="Hledat v ARESu" onClick="@command('searchAres')" sclass="nicebutton" disabled="@load((vm.dataBean.ic == 0) &amp;&amp; (empty vm.dataBean.company))" /> <button image="/img/search.png" label="Hledat v ARESu" onClick="@command('searchAres')" sclass="nicebutton" disabled="@load((vm.dataBean.ic == 0) &amp;&amp; (empty vm.dataBean.company))" />
</row> </row>
<row> <row>
@ -33,14 +31,13 @@
<label value="Kontaktní osoba" /> <textbox value="@bind(vm.dataBean.contactName)"/> <label value="Kontaktní osoba" /> <textbox value="@bind(vm.dataBean.contactName)"/>
</row> </row>
<row> <row>
<label value="Ulice" /> <textbox value="@bind(vm.dataBean.street)"/> <label value="Ulice" /> <textbox value="@bind(vm.dataBean.street)" width="320px"/>
</row> </row>
<row> <row>
<label value="Číslo domu" /> <textbox value="@bind(vm.dataBean.houseNumber)"/> <label value="Číslo domu" /> <textbox value="@bind(vm.dataBean.houseNumber)" width="80px"/>
</row> </row>
<row> <row>
<label value="Město" /> <textbox value="@bind(vm.dataBean.city)"/> <label value="Město" /> <textbox id="city" constraint="@load(vm.constriant)" value="@bind(vm.dataBean.city)" width="320px"/>
<label visible="true" value="@load(vm.errMessages['city'])" style="color:red"/>
</row> </row>
<row> <row>
<label value="PSČ" /> <textbox value="@bind(vm.dataBean.zipCode)"/> <label value="PSČ" /> <textbox value="@bind(vm.dataBean.zipCode)"/>
@ -49,12 +46,10 @@
<label value="Telefon" /> <textbox value="@bind(vm.dataBean.phone)"/> <label value="Telefon" /> <textbox value="@bind(vm.dataBean.phone)"/>
</row> </row>
<row> <row>
<label value="E-mail" /> <textbox value="@bind(vm.dataBean.email)"/> <label value="E-mail" /> <textbox id="email" constraint="@load(vm.constriant)" value="@bind(vm.dataBean.email)" width="320px"/>
<label visible="true" value="@load(vm.errMessages['email'])" style="color:red"/>
</row> </row>
<row> <row>
<label value="Web" /> <textbox value="@bind(vm.dataBean.web)"/> <label value="Web" /> <textbox id="web" constraint="@load(vm.constriant)" value="@bind(vm.dataBean.web)" width="320px"/>
<label visible="true" value="@load(vm.errMessages['web'])" style="color:red"/>
</row> </row>
</rows> </rows>
</grid> </grid>

@ -8,16 +8,12 @@
<columns> <columns>
<column align="right" hflex="min" /> <column align="right" hflex="min" />
<column /> <column />
<column />
</columns> </columns>
<rows> <rows>
<row> <row>
<cell sclass="row-title">${labels.BuildingsFormCode} :</cell> <cell sclass="row-title">${labels.BuildingsFormCode} :</cell>
<cell> <cell>
<textbox width="200px" value="@bind(vm.dataBean.code)" /> <textbox id="code" constraint="@load(vm.constriant)" width="200px" value="@bind(vm.dataBean.code)" />
</cell>
<cell>
<label visible="true" value="@load(vm.errMessages['code'])" style="color:red" />
</cell> </cell>
</row> </row>
<row> <row>

Loading…
Cancel
Save