Skip to content
This repository was archived by the owner on Jan 10, 2019. It is now read-only.

Commit ac67cff

Browse files
committed
Merge pull request #24 from bouil/master
Add class="controls" for <select> elements in forms + Handing of t:errors, and handle errors after form submit
2 parents 3c0b966 + 66002bb commit ac67cff

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

src/main/java/com/trsvax/bootstrap/AbstractFrameworkProvider.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ public boolean anchor(Element element) {
3838
public boolean input(Element element) {
3939
return hasName("input", element);
4040
}
41+
public boolean select(Element element) {
42+
return hasName("select", element);
43+
}
4144
public boolean label(Element element) {
4245
return hasName("label", element);
4346
}

src/main/java/com/trsvax/bootstrap/services/bootstrapprovider/BootstrapFrameworkVisitor.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ Visitor beanEditForm() {
126126
return new Visitor() {
127127

128128
public void visit(Element element) {
129+
if (div(element) && hasClass("t-error", element)) {
130+
element.forceAttributes("class", "alert alert-error");
131+
}
129132
if (hasClass("t-beaneditor", element)) {
130133
pop(element);
131134
element.visit(beanEditForm());
@@ -134,6 +137,9 @@ public void visit(Element element) {
134137
if (hasClass("t-beaneditor-row", element)) {
135138
element.forceAttributes("class", "control-group");
136139
}
140+
if ( select(element)) {
141+
element.wrap("div", "class", "control");
142+
}
137143
if ( input(element)) {
138144

139145
String type= element.getAttribute("type");
@@ -832,6 +838,9 @@ boolean anchor(Element element) {
832838
boolean input(Element element) {
833839
return hasName("input", element);
834840
}
841+
boolean select(Element element) {
842+
return hasName("select", element);
843+
}
835844
boolean label(Element element) {
836845
return hasName("label", element);
837846
}

src/main/java/com/trsvax/bootstrap/services/bootstrapprovider/FormProvider.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.HashSet;
44
import java.util.Set;
55

6+
import org.apache.commons.lang.StringUtils;
67
import org.apache.tapestry5.MarkupWriter;
78
import org.apache.tapestry5.corelib.components.BeanEditForm;
89
import org.apache.tapestry5.dom.Element;
@@ -65,13 +66,19 @@ public void visit(Element element) {
6566
element.addClassName(type);
6667
return;
6768
}
68-
69+
if (div(element) && hasClass("t-error", element)) {
70+
element.forceAttributes("class", "alert alert-error");
71+
}
6972
if (hasClass("t-beaneditor", element)) {
7073
pop.add(element);
7174
}
7275
if (hasClass("t-beaneditor-row", element)) {
7376
element.forceAttributes("class", "control-group");
7477
}
78+
if ( select(element)) {
79+
controls = element.wrap("div", "class", "controls");
80+
markErrors(element);
81+
}
7582
if ( input(element)) {
7683

7784
String type= element.getAttribute("type");
@@ -84,10 +91,12 @@ public void visit(Element element) {
8491
element.addClassName("btn");
8592
} else {
8693
controls = element.wrap("div", "class", "controls");
94+
markErrors(element);
8795
}
8896
}
8997
if ( textarea(element) ) {
9098
element.wrap("div", "class", "controls");
99+
markErrors(element);
91100
}
92101
if ( label(element)) {
93102
element.addClassName("control-label");
@@ -96,6 +105,23 @@ public void visit(Element element) {
96105
element.moveToBottom(controls);
97106
}
98107
}
108+
109+
/**
110+
* If the element is marked as error by Tapesty, add the CSS class "error" to the control-group DIV.
111+
*
112+
* Moves the "help-inline" error message created but the ValidationDecorator as a child of the control DIV.
113+
*
114+
* @param element The input/select element to check for error
115+
*/
116+
private void markErrors(Element element) {
117+
if (hasClass("error", element)){
118+
controls.getContainer().addClassName("error");
119+
Element helpInline = controls.getContainer().getElementByAttributeValue("class", "help-inline");
120+
if (helpInline != null){
121+
helpInline.moveAfter(element);
122+
}
123+
}
124+
}
99125
}
100126

101127

0 commit comments

Comments
 (0)