Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix validation classes when using b:formGroup #689

Merged
merged 3 commits into from
Apr 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.render.FacesRenderer;
import net.bootsfaces.render.CoreInputRenderer;

import net.bootsfaces.render.CoreRenderer;
import net.bootsfaces.render.IResponsive;
Expand All @@ -32,7 +33,7 @@

/** This class generates the HTML code of <b:badge />. */
@FacesRenderer(componentFamily = "net.bootsfaces.component", rendererType = "net.bootsfaces.component.badge.Badge")
public class BadgeRenderer extends CoreRenderer {
public class BadgeRenderer extends CoreInputRenderer {

/**
* This methods generates the HTML code of the current b:badge.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,7 @@ public void setReadonly(boolean _readonly) {
* @return Returns the value of the attribute, or false, if it hasn't been set by the JSF file.
*/
public boolean isRenderLabel() {
return (boolean) (Boolean) getStateHelper().eval(PropertyKeys.renderLabel, false);
return (boolean) (Boolean) getStateHelper().eval(PropertyKeys.renderLabel, net.bootsfaces.component.ComponentUtils.isRenderLabelDefault());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,9 @@ public void encodeEnd(FacesContext context, UIComponent component) throws IOExce
rw.writeAttribute("id", clientId, "id");
Tooltip.generateTooltip(context, colorPicker, rw);
}
if (colorPicker.isInline()) {
rw.writeAttribute("class", getFormGroupWithFeedback("form-inline", clientId), "class");
LOGGER.warning("The inline attribute of b:inputText is deprecated and generates faulty HTML code. Please use <b:form inline=\"true\"> instead.");
} else {
if(hasToRenderFormGroup(component)) {
rw.writeAttribute("class", getFormGroupWithFeedback("form-group", clientId), "class");
}
}
LOGGER.warning("The inline attribute of b:inputText is deprecated and generates faulty HTML code. Please use <b:form inline=\"true\"> instead.");


rw.writeAttribute("class", getWithFeedback(getInputMode(colorPicker.isInline()), component), "class");

if (label != null) {
rw.startElement("label", component);
rw.writeAttribute("for", "input_" + clientId, "for");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,14 @@ private String encodeHTML(FacesContext fc, ResponseWriter rw, DateTimePicker dtp
}

String divSuffix="";
String errorSeverityClass = getFormGroupWithFeedback("", clientId);
// FacesMessages.getErrorSeverityClass(clientId);
String classesWithFeedback = getWithFeedback(InputMode.DEFAULT, dtp);
if (null != responsiveStyleClass && responsiveStyleClass.trim().length()>0) {
rw.startElement("div", dtp);

if (!isHorizontalForm(dtp)) {
rw.writeAttribute("class", responsiveStyleClass + " form-group " + errorSeverityClass, "class");
rw.writeAttribute("class", responsiveStyleClass + classesWithFeedback, "class");
} else {
if(hasToRenderFormGroup(dtp)) {
rw.writeAttribute("class", "form-group " + errorSeverityClass, "class");
}
rw.writeAttribute("class", classesWithFeedback, "class");
}
rw.writeAttribute("id", clientId, null);
Tooltip.generateTooltip(fc, dtp, rw);
Expand All @@ -181,7 +179,7 @@ private String encodeHTML(FacesContext fc, ResponseWriter rw, DateTimePicker dtp
} else if (label != null) {
rw.startElement("div", dtp);
rw.writeAttribute("id", clientId, null);
rw.writeAttribute("class", "form-group " + errorSeverityClass, "class");
rw.writeAttribute("class", classesWithFeedback, "class");
divSuffix=DTP_OUTER_CONTAINER_SUFFIX;
Tooltip.generateTooltip(fc, dtp, rw);
clientIdHasBeenRendered=true;
Expand All @@ -196,7 +194,7 @@ private String encodeHTML(FacesContext fc, ResponseWriter rw, DateTimePicker dtp
if (label != null) {
rw.startElement("label", dtp);
rw.writeAttribute("for", fieldId, "for"); // "input_" + clientId
generateErrorAndRequiredClassForLabels(dtp, rw, clientId, dtp.getLabelStyleClass());
generateErrorAndRequiredClass(dtp, rw, clientId, dtp.getLabelStyleClass(), Responsive.getResponsiveLabelClass(dtp), "control-label");
writeAttribute(rw, "style", dtp.getLabelStyle());

rw.writeText(label, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import javax.faces.convert.ConverterException;

import net.bootsfaces.C;
import net.bootsfaces.component.formGroup.FormGroup;
import net.bootsfaces.component.icon.IconRenderer;
import net.bootsfaces.listeners.AddResourcesListener;
import net.bootsfaces.render.A;
Expand Down Expand Up @@ -235,6 +236,7 @@ protected void addLabel(ResponseWriter rw, String clientId, String fieldId) thro
rw.startElement("label", this);
rw.writeAttribute("for", clientId, "for");
new CoreRenderer().generateErrorAndRequiredClassForLabels(this, rw, fieldId, getLabelStyleClass() + " control-label");

if (getLabelStyle() != null) {
rw.writeAttribute("style", getLabelStyle(), "style");
}
Expand Down Expand Up @@ -267,7 +269,12 @@ private void encodeHTML(FacesContext fc) throws IOException {
rw.startElement("div", this);
numberOfDivs++;
String errorSeverityClass = BsfUtils.isLegacyFeedbackClassesEnabled() ? "" : FacesMessages.getErrorSeverityClass(clientId);
rw.writeAttribute("class", "form-group " + errorSeverityClass, "class");

String formGroupClass="";
if(!(getParent() instanceof FormGroup)) {
formGroupClass = "form-group";
}
rw.writeAttribute("class", formGroupClass + errorSeverityClass, "class");
if (!idHasBeenRendered) {
rw.writeAttribute("id", clientId, "id");
Tooltip.generateTooltip(fc, this, rw);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,8 @@ public void encodeEnd(FacesContext context, UIComponent component) throws IOExce
Tooltip.generateTooltip(context, inputText, rw);
clientIdHasBeenRendered=true;
}
if (inputText.isInline()) {
rw.writeAttribute("class", getFormGroupWithFeedback("form-inline", clientId), "class");
LOGGER.warning("The inline attribute of b:inputText is deprecated and generates faulty HTML code. Please use <b:form inline=\"true\"> instead.");
} else {
if(hasToRenderFormGroup(component)) {
rw.writeAttribute("class", getFormGroupWithFeedback("form-group", clientId), "class");
}
}

rw.writeAttribute("class", getWithFeedback(getInputMode(inputText.isInline()), component), "class");
}

String fieldId = inputText.getFieldId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,8 @@ public void encodeEnd(FacesContext context, UIComponent component) throws IOExce
Tooltip.generateTooltip(context, inputText, rw);
clientIdHasBeenRendered=true;
}
if (inputText.isInline()) {
rw.writeAttribute("class", getFormGroupWithFeedback("form-inline", clientId), "class");
LOGGER.warning("The inline attribute of b:inputText is deprecated and generates faulty HTML code. Please use <b:form inline=\"true\"> instead.");
} else {
if(hasToRenderFormGroup(component)) {
rw.writeAttribute("class", getFormGroupWithFeedback("form-group", clientId), "class");
}
}

rw.writeAttribute("class", getWithFeedback(getInputMode(inputText.isInline()), component), "class");

if (label != null) {
rw.startElement("label", component);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,7 @@ public void encodeBegin(FacesContext context, UIComponent component) throws IOEx
span = startColSpanDiv(rw, selectBooleanCheckbox);
}
rw.startElement("div", component);
if(hasToRenderFormGroup(component)) {
writeAttribute(rw, "class", getFormGroupWithFeedback("form-group", clientId));
}
writeAttribute(rw,"class", getWithFeedback(InputMode.DEFAULT, component), "class");
addLabel(rw, clientId, selectBooleanCheckbox);

renderInputTag(context, rw, clientId, selectBooleanCheckbox);
Expand Down Expand Up @@ -146,7 +144,7 @@ protected void addLabel(ResponseWriter rw, String clientId, SelectBooleanCheckbo
String label = selectBooleanCheckbox.getLabel();
if (label != null) {
rw.startElement("label", selectBooleanCheckbox);
generateErrorAndRequiredClassForLabels(selectBooleanCheckbox, rw, clientId, selectBooleanCheckbox.getLabelStyleClass());
generateErrorAndRequiredClass(selectBooleanCheckbox, rw, clientId, selectBooleanCheckbox.getLabelStyleClass(), Responsive.getResponsiveLabelClass(selectBooleanCheckbox), "control-label");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've stumbled over this yesterday, too. I guess the code is correct. But can you explain how it works?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest, I have to admit that I don't know. I copied it from another component where it worked.

I know that's not the way to do things, but my priority for now is to fix our markup for 1.1.0. When this is merged, I plan to review all the code and propose a simpler and more unified solution, where cumbersome methods like this would dissapear.

I'm currently working on fixing the responsive classes for the components where it dind't work yet. After that I guess I could start to review code to potentially refactor.

writeAttribute(rw, "style", selectBooleanCheckbox.getLabelStyle());

if (null != selectBooleanCheckbox.getDir()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,8 @@ public void encodeBegin(FacesContext context, UIComponent component) throws IOEx
clientIdHasBeenRendered=true;
}

if (menu.isInline()) {
LOGGER.warning(
"The inline attribute of b:selectMultiMenu is deprecated and generates faulty HTML code. Please use <b:form inline=\"true\"> instead.");
rw.writeAttribute("class", getFormGroupWithFeedback("form-inline", clientId), "class");
} else {
if(hasToRenderFormGroup(component)) {
rw.writeAttribute("class", getFormGroupWithFeedback("form-group", clientId), "class");
}
}

rw.writeAttribute("class", getWithFeedback(getInputMode(menu.isInline()), component), "class");

addLabel(rw, clientId + "Inner", menu);

if (isHorizontalForm(component)) {
Expand Down Expand Up @@ -374,7 +366,10 @@ protected void addLabel(ResponseWriter rw, String clientId, SelectMultiMenu menu
if (label != null) {
rw.startElement("label", menu);
rw.writeAttribute("for", clientId, "for");
generateErrorAndRequiredClassForLabels(menu, rw, clientId, menu.getLabelStyleClass());

generateErrorAndRequiredClass(menu, rw, clientId, menu.getLabelStyleClass(), Responsive.getResponsiveLabelClass(menu), "control-label");

//generateErrorAndRequiredClassForLabels(menu, rw, clientId, "control-label " + menu.getLabelStyleClass());
writeAttribute(rw, "style", menu.getLabelStyle());
rw.writeText(label, null);
rw.endElement("label");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,7 @@ public void encodeBegin(FacesContext context, UIComponent component) throws IOEx
}
rw.startElement("div", menu);

if (menu.isInline()) {
rw.writeAttribute("class", getFormGroupWithFeedback("form-inline", outerClientId), "class");
LOGGER.warning("The inline attribute of b:inputText is deprecated and generates faulty HTML code. Please use <b:form inline=\"true\"> instead.");
} else {
if(hasToRenderFormGroup(component)) {
rw.writeAttribute("class", getFormGroupWithFeedback("form-group", outerClientId), "class");
}
}
rw.writeAttribute("class", getWithFeedback(getInputMode(menu.isInline()), component), "class");
if (!clientIdHasBeenRendered) {
rw.writeAttribute("id", outerClientId, "id");
Tooltip.generateTooltip(context, menu, rw);
Expand Down Expand Up @@ -196,7 +189,7 @@ protected void addLabel(ResponseWriter rw, String clientId, SelectOneMenu menu,
if (label != null) {
rw.startElement("label", menu);
rw.writeAttribute("for", clientId, "for");
generateErrorAndRequiredClassForLabels(menu, rw, outerClientId, "control-label");
generateErrorAndRequiredClass(menu, rw, outerClientId, menu.getLabelStyleClass(), Responsive.getResponsiveLabelClass(menu), "control-label");
writeAttribute(rw, "style", menu.getLabelStyle());
rw.writeText(label, null);
rw.endElement("label");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,7 @@ private void encodeHTML(Slider slider, FacesContext context, ResponseWriter rw)
rw.writeAttribute("id", clientId, "id");
Tooltip.generateTooltip(context, slider, rw);

if (slider.isInline()) {
rw.writeAttribute("class", getFormGroupWithFeedback("form-inline", clientId), "class");
} else {
rw.writeAttribute("class", getFormGroupWithFeedback("form-group", clientId), "class");
}
rw.writeAttribute("class", getWithFeedback(getInputMode(slider.isInline()), slider), "class");
rw.startElement("div", null);
String s = "row " + (isVertical ? "slider-vertical" : "slider");
if (slider.getStyleClass()!=null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,7 @@ private void encodeHTML(Slider2 slider, FacesContext context, ResponseWriter rw)
rw.writeAttribute("id", clientId, "id");
Tooltip.generateTooltip(context, slider, rw);

if (slider.isInline()) {
rw.writeAttribute("class", getFormGroupWithFeedback("form-inline", clientId), "class");
} else {
rw.writeAttribute("class", getFormGroupWithFeedback("form-group", clientId), "class");
}
rw.writeAttribute("class", getWithFeedback(getInputMode(slider.isInline()), slider), "class");
rw.startElement("div", null);
String s = "row " + (isVertical ? "slider2-vertical" : "slider2");
if (slider.getStyleClass()!=null) {
Expand Down
51 changes: 45 additions & 6 deletions src/main/java/net/bootsfaces/render/CoreInputRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,68 @@
*/
package net.bootsfaces.render;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.component.UIComponent;
import net.bootsfaces.component.formGroup.FormGroup;
import net.bootsfaces.utils.BsfUtils;
import net.bootsfaces.utils.FacesMessages;

/**
*
* @author Guillermo González de Agüero
*/
public class CoreInputRenderer extends CoreRenderer {

private static final Logger LOGGER = Logger.getLogger(CoreInputRenderer.class.getName());

public enum InputMode {
@Deprecated
INLINE,
DEFAULT
}

/**
* <p>Check whether the input needs to be wrapped on a div containing the "form-group" class.</p>
*
* <p>
* Check whether the input needs to be wrapped on a div containing the
* "form-group" class.</p>
*
* This method return true in the following cases:
* <ul>
* <li>The direct parent of the component passed as argument is an instance of {@link FormGroup}</li>
* <li>The direct parent of the component passed as argument is an instance
* of {@link FormGroup}</li>
* </ul>
*
*
* In any other case, this method will return false.
*
*
* @param component
* @return
* @return
*/
protected boolean hasToRenderFormGroup(UIComponent component) {
return !(component.getParent() instanceof FormGroup);
}

protected String getWithFeedback(InputMode inputMode, UIComponent component) {
String additionalClass;
switch (inputMode) {
case INLINE:
additionalClass = "form-inline";
LOGGER.log(Level.WARNING, "The inline attribute of {0} class (clienId: {1}) is deprecated and generates faulty HTML code. Please use <b:form inline=\"true\"> instead.", new Object[]{component.getClass().getName(), component.getClientId()});
break;
case DEFAULT:
default:
additionalClass = hasToRenderFormGroup(component) ? "form-group" : "";
break;
}

if (BsfUtils.isLegacyFeedbackClassesEnabled()) {
return additionalClass;
}

return additionalClass + " " + FacesMessages.getErrorSeverityClass(component.getClientId());
}

protected InputMode getInputMode(boolean inline) {
return inline ? InputMode.INLINE : InputMode.DEFAULT;
}
}
9 changes: 8 additions & 1 deletion src/main/java/net/bootsfaces/render/CoreRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ protected void renderPassThruAttributes(FacesContext context, UIComponent compon
}

/**
* @deprecated Use {@link CoreRenderer#generateErrorAndRequiredClass(javax.faces.component.UIInput, javax.faces.context.ResponseWriter, java.lang.String, java.lang.String, java.lang.String, java.lang.String) } instead
*
* Renders the CSS pseudo classes for required fields and for the error
* levels.
*
Expand All @@ -108,6 +110,7 @@ protected void renderPassThruAttributes(FacesContext context, UIComponent compon
* @param clientId
* @throws IOException
*/
@Deprecated
public void generateErrorAndRequiredClassForLabels(UIInput input, ResponseWriter rw, String clientId,
String additionalClass) throws IOException {
String styleClass = getErrorAndRequiredClass(input, clientId);
Expand Down Expand Up @@ -578,11 +581,15 @@ public static boolean beginDisabledFieldset(IContentDisabled component, Response

/**
* Get the main field container
*
* @deprecated Use {@link CoreInputRenderer#getWithFeedback(net.bootsfaces.render.CoreInputRenderer.InputMode, javax.faces.component.UIComponent)} instead
*
* @param additionalClass
* @param clientId
* @return
*/
protected String getFormGroupWithFeedback(String additionalClass, String clientId){
@Deprecated
protected String getFormGroupWithFeedback(String additionalClass, String clientId) {
if (BsfUtils.isLegacyFeedbackClassesEnabled()) {
return additionalClass;
}
Expand Down
Loading