Přepracovaná validace v agendě Dodavatelé.

closes #76
multitenant
Josef Rokos 10 years ago
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;
}
}

@ -28,6 +28,7 @@ public class FormViewModel<T extends DataModel> extends DocumentViewModel
private Service<T> service;
private boolean newRec;
private ServiceConstraint<T> constraint;
private BindingForm<T> dataForm;
@Init
public void initFormViewModel(@ExecutionArgParam("selected") T selected, @ExecutionArgParam("service") Service<T> service)
@ -39,6 +40,9 @@ public class FormViewModel<T extends DataModel> extends DocumentViewModel
constraint = new ServiceConstraint<T>();
constraint.setDataBean(selected);
constraint.setService(service);
dataForm = new BindingForm<T>();
dataForm.setDataBean(selected);
if (selected.getId() == 0 && selected.getCreated() == null)
{
newRec = true;
@ -183,4 +187,8 @@ public class FormViewModel<T extends DataModel> extends DocumentViewModel
return true;
}
public BindingForm<T> getDataForm() {
return dataForm;
}
}

@ -5,6 +5,7 @@ import info.bukova.isspst.services.IsspstException;
import info.bukova.isspst.services.addressbook.AdbService;
import info.bukova.isspst.services.addressbook.AddressFinder;
import info.bukova.isspst.ui.FormViewModel;
import info.bukova.isspst.validators.AddressValidator;
import java.util.HashMap;
import java.util.List;
@ -28,15 +29,18 @@ public class AddressForm extends FormViewModel<Address> {
private AddressFinder addressFinderAres;
@WireVariable
private AddressFinder addressFinderTaxID;
private AddressValidator validator;
@Init(superclass = true)
public void init() {
validator = new AddressValidator();
}
@Command
@NotifyChange("dataBean")
public void searchAres() {
getDataForm().bind();
if (getDataBean().getIc() != 0) {
try {
adbService.fillFoundData(addressFinderTaxID, getDataBean());
@ -74,4 +78,8 @@ public class AddressForm extends FormViewModel<Address> {
window.detach();
}
public AddressValidator getValidator() {
return validator;
}
}

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

@ -1,5 +1,8 @@
package info.bukova.isspst.validators;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import info.bukova.isspst.StringUtils;
import org.slf4j.Logger;
@ -94,4 +97,31 @@ public abstract class BaseValidator extends AbstractValidator
return true;
}
protected boolean validateRegExp(ValidationContext ctx, String property, String errMessage, String componentId, String regExpPattern) {
String value = getStringValue(ctx, property);
if (value != null && !value.isEmpty()) {
Pattern pattern = Pattern.compile(regExpPattern);
Matcher matcher = pattern.matcher(value);
if (!matcher.matches()) {
errorMsg(ctx, StringUtils.localize(errMessage), componentId);
return false;
}
}
return true;
}
protected String getStringValue(ValidationContext ctx, String property) {
Property prop = ctx.getProperties(property)[0];
Object value = prop.getValue();
if (value == null || !(value instanceof String)) {
return null;
}
return (String)value;
}
}

@ -3,9 +3,15 @@ DataTypeErr=Chybný datový typ...
UserPasswordIsEmpty=Uživatelské heslo musí být zadané...
UserPasswordsAreNotSame=Znovu zadané heslo není stejné...
RequirementCenterIsEmpty=Musíte zadat středisko...
TripDateIsEmpty=Musíte zadat datum cesty...
EndDateIsEmpty=Musíte zadat datum konce cesty...
FromIsEmpty=Musíte zadat místo začátku cesty...
ToIsEmpty=Musíte zadat cíl cesty...
VehicleIsEmpty=Musíte zadat dopravní prostředek...
CompanyIsEmpty=Název firmy musí být zadaný...
CityIsEmpty=Město musí být zadané...
EmailFormatIncorrect=E-mailová adresa musí být správně zadaná...
WebUrlFormatIncorrect=Adresa webu musí být správně zadaná...

@ -3,7 +3,7 @@
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
<window id="editWin" border="normal" apply="org.zkoss.bind.BindComposer" viewModel="@id('vm') @init('info.bukova.isspst.ui.addressbook.AddressForm')" closable="true" width="600px">
<caption zclass="form-caption" label="${labels.SuppliersFormTitle}" />
<grid width="580px">
<grid form="@id('fx') @init(vm.dataForm) @load(vm.dataBean) @save(vm.dataBean, before='save') @validator(vm.validator)" width="580px">
<columns>
<column label="" hflex="min" />
<column label="" hflex="min" />
@ -12,52 +12,52 @@
<rows>
<row>
<label value="${labels.SuppliersFormCompany}" />
<textbox id="company" constraint="@load(vm.constriant)" value="@bind(vm.dataBean.company)" instant="true" width="320px" />
<textbox id="company" value="@bind(fx.company)" instant="true" width="320px" />
<button image="/img/search.png" label="${labels.SuppliersFormFindInARES}" onClick="@command('searchAres')" sclass="nicebutton" disabled="@load((vm.dataBean.ic == 0) &amp;&amp; (empty vm.dataBean.company))" />
</row>
<row>
<label value="${labels.SuppliersFormIC}" />
<textbox value="@bind(vm.dataBean.ic)" />
<textbox value="@bind(fx.ic)" instant="true" />
</row>
<row>
<label value="${labels.SuppliersFormDIC}" />
<textbox value="@bind(vm.dataBean.dic)" />
<textbox value="@bind(fx.dic)" />
</row>
<row>
<label value="${labels.SuppliersFormDepartment}" />
<textbox value="@bind(vm.dataBean.department)" />
<textbox value="@bind(fx.department)" />
</row>
<row>
<label value="${labels.SuppliersFormContact}" />
<textbox value="@bind(vm.dataBean.contactName)" />
<textbox value="@bind(fx.contactName)" />
</row>
<row>
<label value="${labels.SuppliersFormStreet}" />
<textbox value="@bind(vm.dataBean.street)" width="320px" />
<textbox value="@bind(fx.street)" width="320px" />
</row>
<row>
<label value="${labels.SuppliersFormNo}" />
<textbox value="@bind(vm.dataBean.houseNumber)" width="80px" />
<textbox value="@bind(fx.houseNumber)" width="80px" />
</row>
<row>
<label value="${labels.SuppliersFormCity}" />
<textbox id="city" constraint="@load(vm.constriant)" value="@bind(vm.dataBean.city)" width="320px" />
<textbox id="city" value="@bind(fx.city)" width="320px" />
</row>
<row>
<label value="${labels.SuppliersFormZIP}" />
<textbox value="@bind(vm.dataBean.zipCode)" />
<textbox value="@bind(fx.zipCode)" />
</row>
<row>
<label value="${labels.SuppliersFormPhone}" />
<textbox value="@bind(vm.dataBean.phone)" />
<textbox value="@bind(fx.phone)" />
</row>
<row>
<label value="${labels.SuppliersFormEmail}" />
<textbox id="email" constraint="@load(vm.constriant)" value="@bind(vm.dataBean.email)" width="320px" />
<textbox id="email" value="@bind(fx.email)" width="320px" />
</row>
<row>
<label value="${labels.SuppliersFormWWW}" />
<textbox id="web" constraint="@load(vm.constriant)" value="@bind(vm.dataBean.web)" width="320px" />
<textbox id="web" value="@bind(fx.web)" width="320px" />
</row>
</rows>
</grid>
@ -72,7 +72,7 @@
</panel>
<panel>
<panelchildren>
<ckeditor height="65px" width="100%" toolbar="Basic" value="@bind(vm.dataBean.description)" />
<ckeditor height="65px" width="100%" toolbar="Basic" value="@bind(fx.description)" />
</panelchildren>
</panel>
</vlayout>

Loading…
Cancel
Save