V agendě uživatelů aplikována validace formuláře.

Closes #98
multitenant
František Přibyl 11 years ago
parent 4a55467e1e
commit a834b91091

@ -55,7 +55,11 @@ public class UserServiceImpl extends AbstractService<User> implements UserServic
@Override @Override
@Transactional @Transactional
public void saveWithPwd(User user, String password) { public void saveWithPwd(User user, String password) {
if ((password != null) && !password.isEmpty())
{
this.setPassword(user, password); this.setPassword(user, password);
}
this.update(user); this.update(user);
} }

@ -1,18 +1,20 @@
package info.bukova.isspst.ui.users; package info.bukova.isspst.ui.users;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.select.annotation.WireVariable;
import info.bukova.isspst.data.User; import info.bukova.isspst.data.User;
import info.bukova.isspst.services.users.RoleService; import info.bukova.isspst.services.users.RoleService;
import info.bukova.isspst.services.users.UserService; import info.bukova.isspst.services.users.UserService;
import info.bukova.isspst.ui.FormViewModel; import info.bukova.isspst.ui.FormViewModel;
import info.bukova.isspst.validators.UserFormValidator;
public class UserForm extends FormViewModel<User> { import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.zkoss.bind.Validator;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.select.annotation.WireVariable;
public class UserForm extends FormViewModel<User>
{
@WireVariable @WireVariable
private RoleService roleService; private RoleService roleService;
@WireVariable @WireVariable
@ -22,77 +24,91 @@ public class UserForm extends FormViewModel<User> {
private UserRoles userRoles; private UserRoles userRoles;
private boolean loginFree; private boolean loginFree;
private Validator userFormValidator;
@Init(superclass = true) @Init(superclass = true)
public void init() { public void init()
{
userRoles = new UserRoles(getDataBean(), roleService.getAll()); userRoles = new UserRoles(getDataBean(), roleService.getAll());
loginFree = true; loginFree = true;
this.setUserFormValidator(new UserFormValidator(getDataBean().getCreated() == null));
} }
public UserRoles getUserRoles() { public UserRoles getUserRoles()
{
return userRoles; return userRoles;
} }
public String getRetPasswd() { public String getRetPasswd()
{
return retPasswd; return retPasswd;
} }
@NotifyChange({"canSave", "pwMatches"}) public void setRetPasswd(String retPasswd)
public void setRetPasswd(String retPasswd) { {
this.retPasswd = retPasswd; this.retPasswd = retPasswd;
} }
public String getPassword() { public String getPassword()
{
return password; return password;
} }
@NotifyChange({"canSave", "pwMatches"}) public void setPassword(String password)
public void setPassword(String password) { {
this.password = password; this.password = password;
} }
@Command @Command
@NotifyChange({"loginFree", "canSave"}) @NotifyChange({ "loginFree", "canSave" })
public void checkLogin() { public void checkLogin()
try { {
try
{
userService.loadUserByUsername(getDataBean().getUsername()); userService.loadUserByUsername(getDataBean().getUsername());
loginFree = false; loginFree = false;
} catch(UsernameNotFoundException e) { } catch (UsernameNotFoundException e)
{
loginFree = true; loginFree = true;
} }
} }
public boolean isPwMatches() { public boolean isLoginFree()
return password.equals(retPasswd); {
}
public boolean isLoginFree() {
return loginFree || isEdit(); return loginFree || isEdit();
} }
public boolean isEdit() { public boolean isEdit()
{
return getDataBean().getCreated() != null; return getDataBean().getCreated() != null;
} }
@Override @Override
protected void doSave() { protected void doSave()
if (!password.isEmpty()) { {
userService.saveWithPwd(getDataBean(), this.password); userService.saveWithPwd(getDataBean(), this.password);
} else {
super.doSave();
}
} }
@Override @Override
protected void doAdd() { protected void doAdd()
if (!password.isEmpty()) { {
userService.setPassword(getDataBean(), password); userService.setPassword(getDataBean(), password);
userService.add(getDataBean()); userService.add(getDataBean());
} }
}
@Override @Override
public boolean isCanSave() { public boolean isCanSave()
return password.equals(retPasswd) && isLoginFree() && getDataBean().getUsername() != null && !getDataBean().getUsername().isEmpty(); {
return isLoginFree() && getDataBean().getUsername() != null && !getDataBean().getUsername().isEmpty();
}
public Validator getUserFormValidator()
{
return userFormValidator;
} }
public void setUserFormValidator(Validator userFormValidator)
{
this.userFormValidator = userFormValidator;
}
} }

@ -0,0 +1,67 @@
package info.bukova.isspst.validators;
import info.bukova.isspst.StringUtils;
import org.slf4j.Logger;
import org.zkoss.bind.ValidationContext;
import org.zkoss.bind.validator.AbstractValidator;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.HtmlBasedComponent;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Messagebox;
public abstract class BaseValidator extends AbstractValidator
{
HtmlBasedComponent htmlComponent;
protected Logger getLogger()
{
return null;
}
public void errorMsg(ValidationContext ctx, String msg, String componentIdFocused)
{
this.addInvalidMessage(ctx, msg);
Logger logger = this.getLogger();
if (logger != null)
{
logger.error(msg);
}
htmlComponent = null;
if ((componentIdFocused != null) && (!componentIdFocused.isEmpty()))
{
Component component = ctx.getBindContext().getComponent().getFellowIfAny(componentIdFocused, true);
if ((component != null) && (component instanceof HtmlBasedComponent))
{
htmlComponent = (HtmlBasedComponent) component;
}
}
Messagebox.show(msg, StringUtils.localize("Error"), Messagebox.OK, Messagebox.ERROR, new EventListener<Event>()
{
public void onEvent(Event evt) throws InterruptedException
{
if (htmlComponent != null)
{
htmlComponent.setFocus(true);
}
}
});
}
public void addErrorMsg(ValidationContext ctx, String msg)
{
this.errorMsg(ctx, msg, null);
}
@Override
public void validate(ValidationContext ctx)
{
}
}

@ -0,0 +1,61 @@
package info.bukova.isspst.validators;
import info.bukova.isspst.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zkoss.bind.Property;
import org.zkoss.bind.ValidationContext;
public class UserFormValidator extends BaseValidator
{
private final static Logger log = LoggerFactory.getLogger(UserFormValidator.class.getName());
@Override
protected Logger getLogger()
{
return log;
}
private boolean recordIsNew;
public UserFormValidator(boolean recordIsNew)
{
this.recordIsNew = recordIsNew;
}
@Override
public void validate(ValidationContext ctx)
{
Property propertyOrig = ctx.getProperties("password")[0];
String passwordOrig = (String) propertyOrig.getValue();
if (passwordOrig == null)
{
passwordOrig = "";
}
if (passwordOrig.isEmpty())
{
if (this.recordIsNew)
{
this.errorMsg(ctx, StringUtils.localize("UserPasswordIsEmpty"), "idUserPasswordOriginal");
return;
}
}
Property propertyDupl = ctx.getProperties("retPasswd")[0];
String passwordDupl = (String) propertyDupl.getValue();
if (passwordDupl == null)
{
passwordDupl = "";
}
if (!passwordOrig.equals(passwordDupl))
{
this.errorMsg(ctx, StringUtils.localize("UserPasswordsAreNotSame"), "idUserPasswordDuplicate");
return;
}
}
}

@ -0,0 +1,65 @@
package info.bukova.isspst.validators;
import info.bukova.isspst.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zkoss.bind.Property;
import org.zkoss.bind.ValidationContext;
public class UserPasswordDuplValidator extends BaseValidator
{
private final static Logger log = LoggerFactory.getLogger(UserPasswordDuplValidator.class.getName());
@Override
protected Logger getLogger()
{
return log;
}
@Override
public void validate(ValidationContext ctx)
{
return;
}
public void aaa(ValidationContext ctx)
{
Object argOrig = ctx.getBindContext().getValidatorArg("orig");
if (argOrig == null)
{
this.addErrorMsg(ctx, StringUtils.localize("NullPointerErr"));
return;
}
if (!(argOrig instanceof String))
{
this.addErrorMsg(ctx, StringUtils.localize("DataTypeErr"));
return;
}
String propertyNameOrig = (String) argOrig;
Property propertyOrig = ctx.getProperties(propertyNameOrig)[0];
String passwordOrig = (String) propertyOrig.getValue();
if (passwordOrig == null)
{
passwordOrig = "";
}
Property propertyDupl = ctx.getProperty();
String passwordDupl = (String) propertyDupl.getValue();
if (passwordDupl == null)
{
passwordDupl = "";
}
if (!passwordOrig.equals(passwordDupl))
{
this.addErrorMsg(ctx, StringUtils.localize("UserPasswordsAreNotSame"));
}
}
}

@ -0,0 +1,51 @@
package info.bukova.isspst.validators;
import info.bukova.isspst.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zkoss.bind.Property;
import org.zkoss.bind.ValidationContext;
public class UserPasswordOrigValidator extends BaseValidator
{
private final static Logger log = LoggerFactory.getLogger(UserPasswordOrigValidator.class.getName());
@Override
protected Logger getLogger()
{
return log;
}
private boolean recordIsNew;
public UserPasswordOrigValidator(boolean recordIsNew)
{
this.recordIsNew = recordIsNew;
}
@Override
public void validate(ValidationContext ctx)
{
return;
}
public void aaa(ValidationContext ctx)
{
Property propertyOrig = ctx.getProperty();
String passwordOrig = (String) propertyOrig.getValue();
if (passwordOrig == null)
{
passwordOrig = "";
}
if (passwordOrig.isEmpty())
{
if (this.recordIsNew)
{
this.addErrorMsg(ctx, StringUtils.localize("UserPasswordIsEmpty"));
}
}
}
}

@ -1,3 +1,11 @@
BuildingsFormCodeConstr = Zadejte k\u00F3d budovy... BuildingsFormCodeConstr=Zadejte k\u00F3d budovy...
MaterialFormCodeConstr = Zadejte k\u00F3d materi\u00E1lu... MaterialFormCodeConstr=Zadejte k\u00F3d materi\u00E1lu...
MUnitsFormCodeConstr=Zadejte k\u00f3d m\u011brn\u00e9 jednotky... MUnitsFormCodeConstr=Zadejte k\u00f3d m\u011brn\u00e9 jednotky...
Lad = \u00E1
Led = \u00E9
Leh = \u011B
Lid = \u00ED
Lod = \u00F3
Lyd = \u00FD
Lzh = \u017E

@ -0,0 +1,5 @@
NullPointerErr=Chyba ukazatele...
DataTypeErr=Chybný datový typ...
UserPasswordIsEmpty=Uživatelské heslo musí být zadané...
UserPasswordsAreNotSame=Znovu zadané heslo není stejné...

@ -21,6 +21,7 @@
<system-config> <system-config>
<label-location>/WEB-INF/locales/zk-label.properties</label-location> <label-location>/WEB-INF/locales/zk-label.properties</label-location>
<label-location>/WEB-INF/locales/columns.properties</label-location> <label-location>/WEB-INF/locales/columns.properties</label-location>
<label-location>/WEB-INF/locales/validators.properties</label-location>
</system-config> </system-config>
<language-config> <language-config>

@ -1,35 +1,97 @@
<?page title="${labels.UsersFormTitle}" contentType="text/html;charset=UTF-8"?> <?page title="${labels.UsersFormTitle}" contentType="text/html;charset=UTF-8"?>
<zk> <zk>
<window id="editWin" border="normal" closable="true" width="450px" apply="org.zkoss.bind.BindComposer" <window
viewModel="@id('vm') @init('info.bukova.isspst.ui.users.UserForm')"> id="editWin"
border="normal"
<caption zclass="form-caption" label="${labels.UsersFormTitle}" /> closable="true"
<grid width="440px"> width="450px"
apply="org.zkoss.bind.BindComposer"
viewModel="@id('vm') @init('info.bukova.isspst.ui.users.UserForm')"
validationMessages="@id('vmsg')">
<caption
zclass="form-caption"
label="${labels.UsersFormTitle}" />
<grid
width="440px"
form="@id('fx') @load(vm.dataBean) @save(vm.dataBean, before='save') @validator(vm.userFormValidator)">
<columns> <columns>
<column hflex="min"></column> <column hflex="min"></column>
<column></column> <column></column>
<column></column> <column></column>
</columns> </columns>
<rows> <rows>
<row><label value="${labels.UsersFormLogin}"/><textbox value="@bind(vm.dataBean.username)" instant="true" disabled="@load(vm.edit)" onChange="@command('checkLogin')"/><label value="Login je obsazený" sclass="error" visible="@load(not vm.loginFree)"/></row> <row>
<row><label value="${labels.UsersFormFirstName}"/><textbox value="@bind(vm.dataBean.firstName)"/></row> <label value="${labels.UsersFormLogin}" />
<row><label value="${labels.UsersFormSureName}"/><textbox value="@bind(vm.dataBean.lastName)"/></row> <textbox
<row><label value="${labels.UsersFormPersonalID}"/><textbox value="@bind(vm.dataBean.personalNumber)" width="90px"/></row> value="@bind(vm.dataBean.username)"
<row><label value="${labels.UsersFormEmail}"/><textbox value="@bind(vm.dataBean.email)"/></row> instant="true"
<row><label value=""/><checkbox label="${labels.UsersFormSendNotify}" checked="@bind(vm.dataBean.notify)"/></row> disabled="@load(vm.edit)"
<row><label value="${labels.UsersFormPassword}"/><textbox value="@bind(vm.password)" type="password" instant="true"/></row> onChange="@command('checkLogin')" />
<row><label value="${labels.UsersFormRepeatPassword}"/><textbox value="@bind(vm.retPasswd)" type="password" instant="true"/><label value="Hesla nesouhlasí" sclass="error" visible="@load(not vm.pwMatches)"/></row> <label
<row><label value=""/><checkbox label="${labels.UsersFormActive}" checked="@bind(vm.dataBean.enabled)" disabled="@load(vm.dataBean.username eq 'admin')" /></row> value="Login je obsazený"
sclass="error"
visible="@load(not vm.loginFree)" />
</row>
<row>
<label value="${labels.UsersFormFirstName}" />
<textbox value="@bind(vm.dataBean.firstName)" />
</row>
<row>
<label value="${labels.UsersFormSureName}" />
<textbox value="@bind(vm.dataBean.lastName)" />
</row>
<row>
<label value="${labels.UsersFormPersonalID}" />
<textbox
value="@bind(vm.dataBean.personalNumber)"
width="90px" />
</row>
<row>
<label value="${labels.UsersFormEmail}" />
<textbox value="@bind(vm.dataBean.email)" />
</row>
<row>
<label value="" />
<checkbox
label="${labels.UsersFormSendNotify}"
checked="@bind(vm.dataBean.notify)" />
</row>
<row>
<label value="${labels.UsersFormPassword}" />
<textbox
id="idUserPasswordOriginal"
value="@save(vm.password, before='save')"
type="password"
instant="true" />
</row>
<row>
<label value="${labels.UsersFormRepeatPassword}" />
<textbox
id="idUserPasswordDuplicate"
value="@save(vm.retPasswd, before='save')"
type="password"
instant="true" />
</row>
<row>
<label value="" />
<checkbox
label="${labels.UsersFormActive}"
checked="@bind(vm.dataBean.enabled)"
disabled="@load(vm.dataBean.username eq 'admin')" />
</row>
</rows> </rows>
</grid> </grid>
<groupbox mold="3d"> <groupbox mold="3d">
<caption label="Role"/> <caption label="Role" />
<vbox children="@load(vm.userRoles.roleChecks)"> <vbox children="@load(vm.userRoles.roleChecks)">
<template name="children"> <template name="children">
<checkbox label="@load(each.role.description)" checked="@bind(each.checked)" disabled="@load(vm.dataBean.username eq 'admin')" /> <checkbox
label="@load(each.role.description)"
checked="@bind(each.checked)"
disabled="@load(vm.dataBean.username eq 'admin')" />
</template> </template>
</vbox> </vbox>
</groupbox> </groupbox>
<include src="/app/formButtons.zul" /> <include src="/app/formButtons.zul" />
</window> </window>
</zk> </zk>
Loading…
Cancel
Save