use of org.apache.wicket.validation.INullAcceptingValidator in project projectforge by micromata.
the class UserEditForm method addPasswordFields.
@SuppressWarnings("serial")
private void addPasswordFields() {
// Password
final FieldsetPanel fs = gridBuilder.newFieldset(getString("password"), getString("passwordRepeat"));
final PasswordTextField passwordField = new PasswordTextField(fs.getTextFieldId(), new PropertyModel<>(this, "password")) {
@Override
protected void onComponentTag(final ComponentTag tag) {
super.onComponentTag(tag);
if (passwordUser == null) {
tag.put("value", "");
}
}
};
passwordField.setMarkupId("password").setOutputMarkupId(true);
passwordField.setResetPassword(false).setRequired(isNew());
// Password repeat
final PasswordTextField passwordRepeatField = new PasswordTextField(fs.getTextFieldId(), new PropertyModel<>(this, "passwordRepeat")) {
@Override
protected void onComponentTag(final ComponentTag tag) {
super.onComponentTag(tag);
if (passwordUser == null) {
tag.put("value", "");
}
}
};
passwordRepeatField.setMarkupId("passwordRepeat").setOutputMarkupId(true);
passwordRepeatField.setResetPassword(false).setRequired(false);
// validation
passwordRepeatField.add((INullAcceptingValidator<String>) validatable -> {
final String passwordRepeatInput = validatable.getValue();
passwordField.validate();
final String passwordInput = passwordField.getConvertedInput();
if (StringUtils.isEmpty(passwordInput) == true && StringUtils.isEmpty(passwordRepeatInput) == true) {
passwordUser = null;
return;
}
if (StringUtils.equals(passwordInput, passwordRepeatInput) == false) {
passwordUser = null;
validatable.error(new ValidationError().addKey("user.error.passwordAndRepeatDoesNotMatch"));
return;
}
if (passwordUser == null) {
final List<I18nKeyAndParams> errorMsgKeys = passwordQualityService.checkPasswordQuality(passwordInput.toCharArray());
if (errorMsgKeys.isEmpty() == false) {
for (I18nKeyAndParams errorMsgKey : errorMsgKeys) {
final String localizedMessage = I18nHelper.getLocalizedMessage(errorMsgKey);
validatable.error(new ValidationError().setMessage(localizedMessage));
}
} else {
passwordUser = new PFUserDO();
char[] pw = passwordInput.toCharArray();
userService.createEncryptedPassword(passwordUser, pw);
LoginHandler.clearPassword(pw);
}
}
});
WicketUtils.setPercentSize(passwordField, 50);
WicketUtils.setPercentSize(passwordRepeatField, 50);
fs.add(passwordField);
fs.add(passwordRepeatField);
final I18nKeyAndParams passwordQualityI18nKeyAndParams = passwordQualityService.getPasswordQualityI18nKeyAndParams();
fs.addHelpIcon(I18nHelper.getLocalizedMessage(passwordQualityI18nKeyAndParams));
}
use of org.apache.wicket.validation.INullAcceptingValidator in project projectforge by micromata.
the class UserEditForm method addWlanPasswordFields.
private void addWlanPasswordFields() {
// wlan password
final FieldsetPanel fs = gridBuilder.newFieldset(getString("ldap.wlanSambaPassword"), getString("passwordRepeat"));
final PasswordTextField passwordField = new PasswordTextField(fs.getTextFieldId(), new PropertyModel<>(this, "wlanPassword")) {
@Override
protected void onComponentTag(final ComponentTag tag) {
super.onComponentTag(tag);
if (wlanPasswordValid == false) {
tag.put("value", "");
}
}
};
passwordField.setMarkupId("wlanPassword").setOutputMarkupId(true);
passwordField.setResetPassword(false).setRequired(isNew());
// wlan password repeat
final PasswordTextField passwordRepeatField = new PasswordTextField(fs.getTextFieldId(), new PropertyModel<>(this, "wlanPasswordRepeat")) {
@Override
protected void onComponentTag(final ComponentTag tag) {
super.onComponentTag(tag);
if (wlanPasswordValid == false) {
tag.put("value", "");
}
}
};
passwordRepeatField.setMarkupId("wlanPasswordRepeat").setOutputMarkupId(true);
passwordRepeatField.setResetPassword(false).setRequired(false);
// validation
passwordRepeatField.add((INullAcceptingValidator<String>) validatable -> {
wlanPasswordValid = false;
final String passwordRepeatInput = validatable.getValue();
passwordField.validate();
final String passwordInput = passwordField.getConvertedInput();
if (StringUtils.isEmpty(passwordInput) && StringUtils.isEmpty(passwordRepeatInput)) {
return;
}
if (StringUtils.equals(passwordInput, passwordRepeatInput) == false) {
validatable.error(new ValidationError().addKey("user.error.passwordAndRepeatDoesNotMatch"));
return;
}
final List<I18nKeyAndParams> errorMsgKeys = passwordQualityService.checkPasswordQuality(passwordInput.toCharArray());
if (errorMsgKeys.isEmpty() == false) {
for (I18nKeyAndParams errorMsgKey : errorMsgKeys) {
final String localizedMessage = I18nHelper.getLocalizedMessage(errorMsgKey);
validatable.error(new ValidationError().setMessage(localizedMessage));
}
} else {
wlanPasswordValid = true;
}
});
WicketUtils.setPercentSize(passwordField, 50);
WicketUtils.setPercentSize(passwordRepeatField, 50);
fs.add(passwordField);
fs.add(passwordRepeatField);
final I18nKeyAndParams passwordQualityI18nKeyAndParams = passwordQualityService.getPasswordQualityI18nKeyAndParams();
fs.addHelpIcon(I18nHelper.getLocalizedMessage(passwordQualityI18nKeyAndParams));
}
Aggregations