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 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;
}
}

@ -12,12 +12,10 @@
<column label="" hflex="min"/>
<column label="" hflex="min"/>
<column label=""/>
<column label=""/>
</columns>
<rows>
<row>
<label value="Firma" /> <textbox value="@bind(vm.dataBean.company)" instant="true"/>
<label visible="true" value="@load(vm.errMessages['company'])" style="color:red"/>
<label value="Firma" /> <textbox id="company" constraint="@load(vm.constriant)" value="@bind(vm.dataBean.company)" instant="true" width="320px"/>
<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>
@ -33,14 +31,13 @@
<label value="Kontaktní osoba" /> <textbox value="@bind(vm.dataBean.contactName)"/>
</row>
<row>
<label value="Ulice" /> <textbox value="@bind(vm.dataBean.street)"/>
<label value="Ulice" /> <textbox value="@bind(vm.dataBean.street)" width="320px"/>
</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>
<label value="Město" /> <textbox value="@bind(vm.dataBean.city)"/>
<label visible="true" value="@load(vm.errMessages['city'])" style="color:red"/>
<label value="Město" /> <textbox id="city" constraint="@load(vm.constriant)" value="@bind(vm.dataBean.city)" width="320px"/>
</row>
<row>
<label value="PSČ" /> <textbox value="@bind(vm.dataBean.zipCode)"/>
@ -49,12 +46,10 @@
<label value="Telefon" /> <textbox value="@bind(vm.dataBean.phone)"/>
</row>
<row>
<label value="E-mail" /> <textbox value="@bind(vm.dataBean.email)"/>
<label visible="true" value="@load(vm.errMessages['email'])" style="color:red"/>
<label value="E-mail" /> <textbox id="email" constraint="@load(vm.constriant)" value="@bind(vm.dataBean.email)" width="320px"/>
</row>
<row>
<label value="Web" /> <textbox value="@bind(vm.dataBean.web)"/>
<label visible="true" value="@load(vm.errMessages['web'])" style="color:red"/>
<label value="Web" /> <textbox id="web" constraint="@load(vm.constriant)" value="@bind(vm.dataBean.web)" width="320px"/>
</row>
</rows>
</grid>

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

Loading…
Cancel
Save