KEYCLOAK-1113 - user registration form input boxes and labels are red on

error (css class is configurable in theme, has-error used for
patternfly)
This commit is contained in:
Vlastimil Elias 2015-03-30 09:05:48 +02:00
parent 8727aef647
commit 31a80a973c
4 changed files with 88 additions and 15 deletions

View file

@ -7,7 +7,7 @@
<#elseif section = "form">
<form id="kc-register-form" class="${properties.kcFormClass!}" action="${url.registrationAction}" method="post">
<#if !realm.registrationEmailAsUsername>
<div class="${properties.kcFormGroupClass!}">
<div class="${properties.kcFormGroupClass!} ${messagesPerField.printIfExists('username',properties.kcFormGroupErrorClass!)}">
<div class="${properties.kcLabelWrapperClass!}">
<label for="username" class="${properties.kcLabelClass!}">${msg("username")}</label>
</div>
@ -16,7 +16,7 @@
</div>
</div>
</#if>
<div class="${properties.kcFormGroupClass!}">
<div class="${properties.kcFormGroupClass!} ${messagesPerField.printIfExists('firstName',properties.kcFormGroupErrorClass!)}">
<div class="${properties.kcLabelWrapperClass!}">
<label for="firstName" class="${properties.kcLabelClass!}">${msg("firstName")}</label>
</div>
@ -25,7 +25,7 @@
</div>
</div>
<div class="${properties.kcFormGroupClass!}">
<div class="${properties.kcFormGroupClass!} ${messagesPerField.printIfExists('lastName',properties.kcFormGroupErrorClass!)}">
<div class="${properties.kcLabelWrapperClass!}">
<label for="lastName" class="${properties.kcLabelClass!}">${msg("lastName")}</label>
</div>
@ -34,7 +34,7 @@
</div>
</div>
<div class="${properties.kcFormGroupClass!}">
<div class="${properties.kcFormGroupClass!} ${messagesPerField.printIfExists('email',properties.kcFormGroupErrorClass!)}">
<div class="${properties.kcLabelWrapperClass!}">
<label for="email" class="${properties.kcLabelClass!}">${msg("email")}</label>
</div>
@ -43,7 +43,7 @@
</div>
</div>
<div class="${properties.kcFormGroupClass!}">
<div class="${properties.kcFormGroupClass!} ${messagesPerField.printIfExists('password',properties.kcFormGroupErrorClass!)}">
<div class="${properties.kcLabelWrapperClass!}">
<label for="password" class="${properties.kcLabelClass!}">${msg("password")}</label>
</div>
@ -52,7 +52,7 @@
</div>
</div>
<div class="${properties.kcFormGroupClass!}">
<div class="${properties.kcFormGroupClass!} ${messagesPerField.printIfExists('password-confirm',properties.kcFormGroupErrorClass!)}">
<div class="${properties.kcLabelWrapperClass!}">
<label for="password-confirm" class="${properties.kcLabelClass!}">${msg("passwordConfirm")}</label>
</div>

View file

@ -18,6 +18,7 @@ kcFormAreaClass=col-xs-12 col-sm-8 col-md-8 col-lg-6 login
kcFormClass=form-horizontal
kcFormGroupClass=form-group
kcFormGroupErrorClass=has-error
kcLabelClass=control-label
kcLabelWrapperClass=col-xs-12 col-sm-12 col-md-4 col-lg-3
kcInputClass=form-control

View file

@ -16,6 +16,7 @@ import org.keycloak.login.freemarker.model.CodeBean;
import org.keycloak.freemarker.beans.LocaleBean;
import org.keycloak.login.freemarker.model.LoginBean;
import org.keycloak.login.freemarker.model.MessageBean;
import org.keycloak.login.freemarker.model.MessagesPerFieldBean;
import org.keycloak.login.freemarker.model.OAuthGrantBean;
import org.keycloak.login.freemarker.model.ProfileBean;
import org.keycloak.login.freemarker.model.RealmBean;
@ -186,25 +187,20 @@ import java.util.concurrent.TimeUnit;
messagesBundle = new Properties();
}
MessagesPerFieldBean messagesPerField = new MessagesPerFieldBean();
if (messages != null) {
Map<String, MessageBean> messagesPerField = new HashMap<String, MessageBean>();
MessageBean wholeMessage = new MessageBean(null, messageType);
for (FormMessage message : this.messages) {
String formattedMessageText = formatMessageMessage(message, messagesBundle, locale);
if (formattedMessageText != null) {
wholeMessage.appendSummaryLine(formattedMessageText);
MessageBean fm = messagesPerField.get(message.getField());
if (fm == null) {
messagesPerField.put(message.getField(), new MessageBean(formattedMessageText, messageType));
} else {
fm.appendSummaryLine(formattedMessageText);
}
messagesPerField.addMessage(message.getField(), formattedMessageText, messageType);
}
}
attributes.put("message", wholeMessage);
attributes.put("messagePerField", messagesPerField);
}
attributes.put("messagesPerField", messagesPerField);
if (page == LoginFormsPages.OAUTH_GRANT) {
// for some reason Resteasy 2.3.7 doesn't like query params and form params with the same name and will null out the code form param
uriBuilder.replaceQuery(null);

View file

@ -0,0 +1,76 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2015 Red Hat Inc. and/or its affiliates and other contributors
* as indicated by the @authors tag. All rights reserved.
*/
package org.keycloak.login.freemarker.model;
import java.util.HashMap;
import java.util.Map;
import org.keycloak.login.FormMessage;
import org.keycloak.login.freemarker.FreeMarkerLoginFormsProvider.MessageType;
/**
* Bean used to hold form messages per field. Stored under <code>messagesPerField</code> key in Freemarker context.
*
* @author Vlastimil Elias (velias at redhat dot com)
*/
public class MessagesPerFieldBean {
private Map<String, MessageBean> messagesPerField = new HashMap<String, MessageBean>();
public void addMessage(String field, String messageText, MessageType messageType) {
if (messageText == null || messageText.trim().isEmpty())
return;
if (field == null)
field = FormMessage.GLOBAL;
MessageBean fm = messagesPerField.get(field);
if (fm == null) {
messagesPerField.put(field, new MessageBean(messageText, messageType));
} else {
fm.appendSummaryLine(messageText);
}
}
/**
* Check if message for given field exists
*
* @param field
* @return
*/
public boolean exists(String field) {
return messagesPerField.containsKey(field);
}
/**
* Get message for given field.
*
* @param fieldName
* @return message text or empty string
*/
public String get(String fieldName) {
MessageBean mb = messagesPerField.get(fieldName);
if (mb != null) {
return mb.getSummary();
} else {
return "";
}
}
/**
* Print text if message for given field exists. Useful eg. to add css styles for fields with message.
*
* @param fieldName to check for
* @param text to print
* @return text if message exists for given field, else empty string
*/
public String printIfExists(String fieldName, String text) {
if (exists(fieldName))
return text;
else
return "";
}
}