Skip to content

fix: add manual validation property to custom field #7567

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 @@ -15,17 +15,23 @@
*/
package com.vaadin.flow.component.customfield.tests;

import java.util.concurrent.atomic.AtomicInteger;

import com.vaadin.flow.component.customfield.CustomField;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.NativeButton;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.component.textfield.IntegerField;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.data.binder.Binder;
import com.vaadin.flow.router.Route;

@Route("vaadin-custom-field/validation")
public class ValidationPage extends Div {
public ValidationPage() {
MyField customField = new MyField();
customField.setLabel("Custom field");
customField.setId("custom-field");

NativeButton setInvalid = new NativeButton("Set invalid", e -> {
customField.setInvalid(true);
Expand Down Expand Up @@ -53,6 +59,23 @@ public ValidationPage() {

add(customField);
add(new Div(setInvalid, attach, detach, logInvalidState, logOutput));

var customFieldWithDelegatedValidation = new CustomFieldWithDelegatedValidation();
customFieldWithDelegatedValidation
.setLabel("CustomField with delegated validation");
customFieldWithDelegatedValidation
.setId("custom-field-with-delegated-validation");

var binder = new Binder<AtomicInteger>();
binder.forField(customFieldWithDelegatedValidation)
.asRequired("Cannot be empty")
.bind(AtomicInteger::get, AtomicInteger::set);

var validate = new NativeButton("Validate", e -> binder.validate());
validate.setId("validate");

add(customFieldWithDelegatedValidation);
add(new Div(validate));
}

private class MyField extends CustomField<Integer> {
Expand All @@ -78,7 +101,45 @@ protected Integer generateModelValue() {

@Override
protected void setPresentationValue(Integer integer) {
}
}

private static class CustomFieldWithDelegatedValidation
extends CustomField<Integer> {
private final IntegerField field = new IntegerField();

CustomFieldWithDelegatedValidation() {
add(field);
}

@Override
protected Integer generateModelValue() {
return field.getValue();
}

@Override
protected void setPresentationValue(Integer newPresentationValue) {
field.setValue(newPresentationValue);
}

@Override
public void setErrorMessage(String errorMessage) {
field.setErrorMessage(errorMessage);
}

@Override
public String getErrorMessage() {
return field.getErrorMessage();
}

@Override
public void setInvalid(boolean invalid) {
field.setInvalid(invalid);
}

@Override
public boolean isInvalid() {
return field.isInvalid();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,61 +18,72 @@
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Keys;

import com.vaadin.flow.component.customfield.testbench.CustomFieldElement;
import com.vaadin.flow.component.textfield.testbench.IntegerFieldElement;
import com.vaadin.flow.testutil.TestPath;
import com.vaadin.testbench.TestBenchElement;
import com.vaadin.tests.AbstractComponentIT;

@TestPath("vaadin-custom-field/validation")
public class ValidationIT extends AbstractComponentIT {

private TestBenchElement customField;
private TestBenchElement setInvalid;
private TestBenchElement attach;
private TestBenchElement detach;
private TestBenchElement logInvalidState;
private CustomFieldElement customField;
private TestBenchElement logOutput;

private CustomFieldElement customFieldWithDelegatedValidation;

@Before
public void init() {
open();

customField = $("vaadin-custom-field").waitForFirst();
setInvalid = $("button").id("set-invalid");
attach = $("button").id("attach");
detach = $("button").id("detach");
logInvalidState = $("button").id("log-invalid-state");
customField = $(CustomFieldElement.class).id("custom-field");
logOutput = $("span").id("log-output");
customFieldWithDelegatedValidation = $(CustomFieldElement.class)
.id("custom-field-with-delegated-validation");
}

@Test
public void overridesClientValidation() {
setInvalid.click();
clickElementWithJs("set-invalid");

executeScript("arguments[0].validate()", customField);
executeScript("arguments[0]._requestValidation()", customField);

Assert.assertEquals(true, customField.getPropertyBoolean("invalid"));
}

@Test
public void detach_reattach_overridesClientValidation() {
setInvalid.click();
detach.click();
attach.click();
clickElementWithJs("set-invalid");
clickElementWithJs("detach");
clickElementWithJs("attach");

customField = $("vaadin-custom-field").waitForFirst();
executeScript("arguments[0].validate()", customField);
customField = $(CustomFieldElement.class).id("custom-field");
executeScript("arguments[0]._requestValidation()", customField);

Assert.assertEquals(true, customField.getPropertyBoolean("invalid"));
}

@Test
public void changeInvalidOnClient_notSynchronizedToServer() {
setInvalid.click();
clickElementWithJs("set-invalid");

executeScript("arguments[0].invalid = false", customField);
logInvalidState.click();
clickElementWithJs("log-invalid-state");

Assert.assertEquals("true", logOutput.getText());
}

@Test
public void delegatedValidation_initiallyInvalid_focus_blur_noClientValidation() {
clickElementWithJs("validate");
customFieldWithDelegatedValidation.focus();
customFieldWithDelegatedValidation.sendKeys(Keys.TAB);

var innerField = customFieldWithDelegatedValidation
.$(IntegerFieldElement.class).first();

Assert.assertTrue(innerField.getPropertyBoolean("invalid"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.slf4j.LoggerFactory;

import com.vaadin.flow.component.AbstractField;
import com.vaadin.flow.component.AttachEvent;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.Focusable;
import com.vaadin.flow.component.HasTheme;
Expand Down Expand Up @@ -73,13 +72,7 @@ public CustomField(T defaultValue) {
super(defaultValue);
// Force a value update when the change event generated
getElement().addEventListener("change", e -> this.updateValue());
}

@Override
protected void onAttach(AttachEvent attachEvent) {
super.onAttach(attachEvent);

FieldValidationUtil.disableClientValidation(this);
getElement().setProperty("manualValidation", true);
}

/**
Expand Down

This file was deleted.