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

Custom Field is not validated properly in Vaadin 24 #5328

Closed
jcgueriaud1 opened this issue Aug 4, 2023 · 2 comments
Closed

Custom Field is not validated properly in Vaadin 24 #5328

jcgueriaud1 opened this issue Aug 4, 2023 · 2 comments

Comments

@jcgueriaud1
Copy link
Contributor

jcgueriaud1 commented Aug 4, 2023

Description

When I open a view with a bound custom field, the validation is running and is invalid but the custom field does not show the error message.
If I'm doing the same thing with a textfield, it's invalid.
image

Expected outcome

I want to see the field invalid like the textfield.

I tried to implement the HasValidator interface but it didn't change anything.
That might be a problem of implementation but there is nothing in the documentation or in the migration guide.

Minimal reproducible example

@PageTitle("Hello World Vaadin 24")
@Route(value = "custom")
@AnonymousAllowed
public class CustomFieldView extends HorizontalLayout {
    private Binder<FakeBean> binder = new Binder<>();
    private TextField name = new TextField("Name");
    private CustomMoneyField id2 = new CustomMoneyField("Id2");

    public CustomFieldView() {
        FakeBean fakeBean = new FakeBean();
        binder.forField(id2).asRequired("id2 is required")
                .withValidator(value -> {
                    System.out.println("Validate id 2: " + value);
                    return (value > 2);
                }, "Value should be > 2")
                .bind(FakeBean::getId2, FakeBean::setId2);
        binder.forField(name).asRequired("name is required")
                .withValidator(value -> {
                    System.out.println("Validate name: " + value);
                    return (value.length() > 2);
                }, "Length should be > 2")
                .bind(FakeBean::getName, FakeBean::setName);
        binder.setBean(fakeBean);
        add(id2, name);
    }
}


package com.example.application.views;

import com.vaadin.flow.component.*;
import com.vaadin.flow.component.customfield.CustomField;
import com.vaadin.flow.component.shared.HasTooltip;
import com.vaadin.flow.component.shared.Tooltip;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.component.textfield.TextFieldVariant;
import com.vaadin.flow.data.binder.HasValidator;
import com.vaadin.flow.data.binder.ValidationStatusChangeEvent;
import com.vaadin.flow.data.binder.ValidationStatusChangeListener;
import com.vaadin.flow.shared.Registration;
import org.apache.commons.lang3.StringUtils;

public class CustomMoneyField extends CustomField<Long> implements HasValidator<Long> {

    private final TextField content = new TextField();

    public CustomMoneyField(String label) {
        super(null);
        add(content);
        getContent().addThemeName("align-right");
        setLabel(label);
    }
    @Override
    protected void setPresentationValue(Long newPresentationValue) {
        getContent().setValue(newPresentationValue + "");
    }


    @Override
    protected Long generateModelValue() {
        return StringUtils.isNotEmpty(getContent().getValue()) ? Long.parseLong(getContent().getValue()) : null;
    }
    public TextField getContent() {
        return content;
    }

    @Override
    public Registration addValidationStatusChangeListener(ValidationStatusChangeListener<Long> listener) {
        return this.getContent().addClientValidatedEventListener((event) -> {
            listener.validationStatusChanged(new ValidationStatusChangeEvent(this, !this.isInvalid()));
        });
    }
}

The code is in this branch: https://github.com/jcgueriaud1/spring-proxy-issue/tree/custom-field

In this page: http://localhost:8080/custom

Steps to reproduce

Open the page, the textfield is invalid with an error message, the custom field is valid without error message.
Both validators are running:

Validate id 2: 1
Validate name: na
Validate name: na

Environment

Vaadin version(s): Vaadin 24.1.4
OS: Mac

Browsers

Issue is not browser related

@jcgueriaud1
Copy link
Contributor Author

jcgueriaud1 commented Aug 4, 2023

In Vaadin 23, both fields are valid:
https://github.com/jcgueriaud1/spring-proxy-issue/tree/custom-field-validation-v23

image

Both validators are running: (but the validator for the text field is running once)

Validate id 2: 1
Validate name: na

@sissbruecker
Copy link
Contributor

Closing as this has the same root cause as #5330

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants