Skip to content

Commit eb50645

Browse files
authored
fix: disable client-side validation for CheckboxGroup (#2975)
* fix: disable client-side validation for CheckboxGroup * run formattewr * fix existing test
1 parent e0c963d commit eb50645

6 files changed

Lines changed: 167 additions & 8 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.vaadin.flow.component.checkbox.tests;
2+
3+
import com.vaadin.flow.component.checkbox.CheckboxGroup;
4+
import com.vaadin.flow.component.html.Div;
5+
import com.vaadin.flow.component.html.NativeButton;
6+
import com.vaadin.flow.component.html.Span;
7+
import com.vaadin.flow.router.Route;
8+
9+
@Route("vaadin-checkbox/validation")
10+
public class ValidationPage extends Div {
11+
public ValidationPage() {
12+
CheckboxGroup<String> checkboxGroup = new CheckboxGroup<>();
13+
checkboxGroup.setItems("foo", "bar");
14+
checkboxGroup.setLabel("CheckboxGroup");
15+
16+
NativeButton setInvalid = new NativeButton("Set invalid", e -> {
17+
checkboxGroup.setInvalid(true);
18+
checkboxGroup.setErrorMessage("Error message from server");
19+
});
20+
setInvalid.setId("set-invalid");
21+
22+
NativeButton attach = new NativeButton("Attach", e -> {
23+
add(checkboxGroup);
24+
});
25+
attach.setId("attach");
26+
27+
NativeButton detach = new NativeButton("Detach", e -> {
28+
remove(checkboxGroup);
29+
});
30+
detach.setId("detach");
31+
32+
Span logOutput = new Span();
33+
logOutput.setId("log-output");
34+
NativeButton logInvalidState = new NativeButton("Log Invalid State",
35+
e -> {
36+
logOutput
37+
.setText(String.valueOf(checkboxGroup.isInvalid()));
38+
});
39+
logInvalidState.setId("log-invalid-state");
40+
41+
add(checkboxGroup);
42+
add(new Div(setInvalid, attach, detach, logInvalidState, logOutput));
43+
}
44+
}

‎vaadin-checkbox-flow-parent/vaadin-checkbox-flow-integration-tests/src/test/java/com/vaadin/flow/component/checkbox/tests/CheckboxGroupIT.java‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,6 @@ public void groupHasLabelAndErrorMessage_setInvalidShowEM_setValueRemoveEM() {
191191
"Correct error message should be shown after the button clicks",
192192
"Field has been set to invalid from server side",
193193
errorMessage.getText());
194-
195-
executeScript("arguments[0].value=['2'];", group);
196-
verifyGroupValid(group, errorMessage);
197194
}
198195

199196
@Test
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.vaadin.flow.component.checkbox.tests;
2+
3+
import com.vaadin.flow.testutil.TestPath;
4+
import com.vaadin.testbench.TestBenchElement;
5+
import com.vaadin.tests.AbstractComponentIT;
6+
import org.junit.Assert;
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
10+
@TestPath("vaadin-checkbox/validation")
11+
public class ValidationIT extends AbstractComponentIT {
12+
13+
private TestBenchElement checkboxGroup;
14+
private TestBenchElement setInvalid;
15+
private TestBenchElement attach;
16+
private TestBenchElement detach;
17+
private TestBenchElement logInvalidState;
18+
private TestBenchElement logOutput;
19+
20+
@Before
21+
public void init() {
22+
open();
23+
checkboxGroup = $("vaadin-checkbox-group").waitForFirst();
24+
setInvalid = $("button").id("set-invalid");
25+
attach = $("button").id("attach");
26+
detach = $("button").id("detach");
27+
logInvalidState = $("button").id("log-invalid-state");
28+
logOutput = $("span").id("log-output");
29+
}
30+
31+
@Test
32+
public void overridesClientValidation() {
33+
setInvalid.click();
34+
35+
executeScript("arguments[0].validate()", checkboxGroup);
36+
37+
Assert.assertEquals(true, checkboxGroup.getPropertyBoolean("invalid"));
38+
}
39+
40+
@Test
41+
public void detach_reattach_overridesClientValidation() {
42+
setInvalid.click();
43+
detach.click();
44+
attach.click();
45+
46+
checkboxGroup = $("vaadin-checkbox-group").waitForFirst();
47+
executeScript("arguments[0].validate()", checkboxGroup);
48+
49+
Assert.assertEquals(true, checkboxGroup.getPropertyBoolean("invalid"));
50+
}
51+
52+
@Test
53+
public void changeInvalidOnClient_notSynchronizedToServer() {
54+
setInvalid.click();
55+
56+
executeScript("arguments[0].invalid = false", checkboxGroup);
57+
logInvalidState.click();
58+
59+
Assert.assertEquals("true", logOutput.getText());
60+
}
61+
}

‎vaadin-checkbox-flow-parent/vaadin-checkbox-flow/src/main/java/com/vaadin/flow/component/checkbox/CheckboxGroup.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.stream.Collectors;
2727
import java.util.stream.Stream;
2828

29+
import com.vaadin.flow.component.AttachEvent;
2930
import com.vaadin.flow.component.Component;
3031
import com.vaadin.flow.component.ComponentUtil;
3132
import com.vaadin.flow.component.HasHelper;
@@ -107,6 +108,13 @@ public CheckboxGroup() {
107108
registerValidation();
108109
}
109110

111+
@Override
112+
protected void onAttach(AttachEvent attachEvent) {
113+
super.onAttach(attachEvent);
114+
115+
FieldValidationUtil.disableClientValidation(this);
116+
}
117+
110118
@Override
111119
public CheckboxGroupDataView<T> setItems(
112120
DataProvider<T, Void> dataProvider) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2000-2022 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.vaadin.flow.component.checkbox;
17+
18+
class FieldValidationUtil {
19+
private FieldValidationUtil() {
20+
}
21+
22+
static <T> void disableClientValidation(CheckboxGroup<T> component) {
23+
// Since this method should be called for every time when the component
24+
// is attached to the UI, lets check that it is actually so
25+
if (!component.isAttached()) {
26+
throw new IllegalStateException(String.format(
27+
"Component %s is not attached. Client side validation "
28+
+ "should be disabled when the component is "
29+
+ "attached, thus this method needs to be called "
30+
+ "from the onAttach method of the component.",
31+
component));
32+
}
33+
// Wait until the response is being written as the validation state
34+
// should not change after that
35+
component.getUI().ifPresent(ui -> ui.beforeClientResponse(component,
36+
executionContext -> overrideClientValidation(component)));
37+
}
38+
39+
private static <T> void overrideClientValidation(
40+
CheckboxGroup<T> component) {
41+
StringBuilder expression = new StringBuilder(
42+
"this.validate = function () {return this.checkValidity();};");
43+
44+
if (component.isInvalid()) {
45+
/*
46+
* By default the invalid flag is set to false. Workaround the case
47+
* where the client side validation overrides the invalid state
48+
* before the validation function itself is overridden above.
49+
*/
50+
expression.append("this.invalid = true;");
51+
}
52+
component.getElement().executeJs(expression.toString());
53+
}
54+
}

‎vaadin-checkbox-flow-parent/vaadin-checkbox-flow/src/main/java/com/vaadin/flow/component/checkbox/GeneratedVaadinCheckboxGroup.java‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -288,14 +288,9 @@ protected void setRequired(boolean required) {
288288
* </p>
289289
* <p>
290290
* This property is set to true when the control value is invalid.
291-
* <p>
292-
* This property is synchronized automatically from client side when a
293-
* 'invalid-changed' event happens.
294-
* </p>
295291
*
296292
* @return the {@code invalid} property from the webcomponent
297293
*/
298-
@Synchronize(property = "invalid", value = "invalid-changed")
299294
protected boolean isInvalidBoolean() {
300295
return getElement().getProperty("invalid", false);
301296
}

0 commit comments

Comments
 (0)