This repository has been archived by the owner on Apr 6, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Disable client-side validation on setRequiredIndicatorVisible() (#77)
Fixes #4077
- Loading branch information
Denis
committed
May 22, 2018
1 parent
e010517
commit 31da851
Showing
15 changed files
with
481 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/main/java/com/vaadin/flow/component/textfield/RequiredValidationUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright 2000-2017 Vaadin Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package com.vaadin.flow.component.textfield; | ||
|
||
import com.vaadin.flow.component.Component; | ||
import com.vaadin.flow.internal.StateNode; | ||
|
||
/** | ||
* Utility class for text field mixin web components to disable/enable client | ||
* side validation. | ||
* | ||
* @author Vaadin Ltd | ||
* | ||
*/ | ||
final class RequiredValidationUtil { | ||
|
||
RequiredValidationUtil() { | ||
// utility class should not be instantiated | ||
} | ||
|
||
static void updateClientValidation(boolean requiredIndicatorVisible, | ||
Component component) { | ||
if (requiredIndicatorVisible) { | ||
disableClientValiation(component); | ||
} else { | ||
enableClientValiation(component); | ||
} | ||
} | ||
|
||
static void disableClientValiation(Component component) { | ||
execJS(component, | ||
"window.Vaadin.Flow.textConnector.disableClientValidation($0);"); | ||
} | ||
|
||
static void enableClientValiation(Component component) { | ||
execJS(component, | ||
"window.Vaadin.Flow.textConnector.enableClientValidation($0);"); | ||
} | ||
|
||
private static void execJS(Component component, String js) { | ||
StateNode node = component.getElement().getNode(); | ||
|
||
node.runWhenAttached(ui -> ui.getInternals().getStateTree() | ||
.beforeClientResponse(node, context -> ui.getPage() | ||
.executeJavaScript(js, component.getElement()))); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/main/resources/META-INF/resources/frontend/textConnector.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
window.Vaadin.Flow.textConnector = { | ||
|
||
disableClientValidation: function (textComponent){ | ||
if ( typeof textComponent.$validation == 'undefined'){ | ||
textComponent.$validation = textComponent.checkValidity; | ||
textComponent.checkValidity = function() { return true; }; | ||
} | ||
}, | ||
|
||
enableClientValidation: function (textComponent){ | ||
if ( textComponent.$validation ){ | ||
textComponent.checkValidity = textComponent.$validation; | ||
delete textComponent.$validation; | ||
} | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
src/test/java/com/vaadin/flow/component/textfield/tests/AbstractRequiredValidationIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 2000-2017 Vaadin Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package com.vaadin.flow.component.textfield.tests; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.openqa.selenium.By; | ||
import org.openqa.selenium.Keys; | ||
import org.openqa.selenium.WebElement; | ||
|
||
import com.vaadin.flow.testutil.AbstractComponentIT; | ||
|
||
public abstract class AbstractRequiredValidationIT extends AbstractComponentIT { | ||
|
||
@Test | ||
public void requiredValidation_disabledWithBinder_enabledViaExpicitCall() | ||
throws InterruptedException { | ||
open(); | ||
|
||
WebElement name = findElement(By.id("name")); | ||
name.sendKeys(Keys.TAB); | ||
Assert.assertFalse(Boolean.parseBoolean(name.getAttribute("invalid"))); | ||
|
||
findElement(By.id("hide")).click(); | ||
|
||
name.sendKeys(Keys.TAB); | ||
|
||
Assert.assertTrue(Boolean.parseBoolean(name.getAttribute("invalid"))); | ||
} | ||
|
||
} |
55 changes: 55 additions & 0 deletions
55
src/test/java/com/vaadin/flow/component/textfield/tests/AbstractRequiredValidationPage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright 2000-2017 Vaadin Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package com.vaadin.flow.component.textfield.tests; | ||
|
||
import com.vaadin.flow.component.Component; | ||
import com.vaadin.flow.component.HasValue; | ||
import com.vaadin.flow.component.html.Div; | ||
import com.vaadin.flow.component.html.NativeButton; | ||
import com.vaadin.flow.data.binder.BeanValidationBinder; | ||
|
||
public abstract class AbstractRequiredValidationPage<T extends HasValue<?, ?>> | ||
extends Div { | ||
|
||
public AbstractRequiredValidationPage() { | ||
T name = createTextField(); | ||
|
||
Entity entity = new Entity(); | ||
BeanValidationBinder<Entity> binder = new BeanValidationBinder<>( | ||
Entity.class); | ||
binder.forField((HasValue<?, ?>) name).bind("name"); | ||
|
||
Component nameComponent = (Component) name; | ||
|
||
nameComponent.setId("name"); | ||
|
||
binder.setBean(entity); | ||
|
||
add(nameComponent); | ||
|
||
NativeButton off = new NativeButton( | ||
"Make required indicator invisible and set requied", event -> { | ||
name.setRequiredIndicatorVisible(false); | ||
setRequired(name); | ||
}); | ||
off.setId("hide"); | ||
add(off); | ||
} | ||
|
||
protected abstract void setRequired(T field); | ||
|
||
protected abstract T createTextField(); | ||
} |
38 changes: 38 additions & 0 deletions
38
src/test/java/com/vaadin/flow/component/textfield/tests/Entity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright 2000-2017 Vaadin Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package com.vaadin.flow.component.textfield.tests; | ||
|
||
import javax.validation.constraints.NotNull; | ||
|
||
public class Entity { | ||
|
||
@NotNull | ||
private String name; | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Entity, name: " + name; | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
...est/java/com/vaadin/flow/component/textfield/tests/PasswordFieldRequiredValidationIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright 2000-2017 Vaadin Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package com.vaadin.flow.component.textfield.tests; | ||
|
||
import com.vaadin.flow.testutil.TestPath; | ||
|
||
/** | ||
* The test code is in the super class. | ||
* | ||
* @author Vaadin Ltd | ||
* | ||
*/ | ||
@TestPath("password-field-required-binder") | ||
public class PasswordFieldRequiredValidationIT | ||
extends AbstractRequiredValidationIT { | ||
|
||
} |
Oops, something went wrong.