alejandro-du / crudui

Automatically generate CRUD-like Vaadin views for any Java Bean
https://vaadin.com/directory#!addon/crud-ui-add-on
Apache License 2.0
85 stars 52 forks source link

Problem with using IntegerField (strange ClassCastException) #136

Open mgodbole opened 2 weeks ago

mgodbole commented 2 weeks ago

Hi! I have just started to learn Vaadin, so please bear with me.

I am using GridCrud (awesome add on Alejandro, thank you!) In the editor, I wish to use the IntegerField as I love the UI (steps, helper text etc). But I am getting a strange (to me at least) ClassCastException.

Here are the details:

The Entity

@Entity
public class Movie {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotNull(message = "Movie name required")
    @NotBlank(message = "Movie name required")
    private String name;

    @NotNull(message = "Rating required")
    @Min(value = 0, message = "Rating should be between 0-10")
    @Max(value = 10, message = "Rating should be between 0-10")
    private Integer rating;

    public Movie() {
    // TODO Auto-generated constructor stub
    }

    public Movie(String name, Integer rating) {
    this.name = name;
    this.rating = rating;
    }

    //Getter setter etc omitted
}

The view

@Route(value = "")
@PageTitle("GridCrud Test")
public class MainView extends VerticalLayout {

    public MainView(MovieService service) {
    var crud = new GridCrud<>(Movie.class, service);

    // Extract form
    var form = crud.getCrudFormFactory();
    form.setUseBeanValidation(true);

    // Customize form
    form.setVisibleProperties("name", "rating");

    form.setFieldProvider("rating", movie -> {
        var ratingField = new IntegerField("Rating");
        ratingField.setMin(0);
        ratingField.setMax(10);
        ratingField.setStepButtonsVisible(true);
        ratingField.setHelperText("Rating out of 10");

        return ratingField;
    });

    setSizeFull();
    add(new H2("Movie database"), crud);
    }

}

The UX

  1. When I click on the "Add" button, the editor pops up and the IntegerField shows up correctly, but after entering the values (validation runs correctly) I get the ClassCastException.
  2. On the other hand if I click on the "Edit" button, the editor does not show at all! (Same exception)

com.vaadin.flow.data.binder.BindingException: An exception has been thrown inside binding logic for the field element [label='Rating']

Caused by: java.lang.ClassCastException: class java.lang.String cannot be cast to class java.lang.Number (java.lang.String and java.lang.Number are in module java.base of loader 'bootstrap') at com.vaadin.flow.component.textfield.AbstractNumberField.setValue(AbstractNumberField.java:40) ~[vaadin-text-field-flow-24.4.9.jar:na] at com.vaadin.flow.data.binder.Binder$BindingImpl.lambda$convertAndSetFieldValue$4(Binder.java:1572) ~[flow-data-24.4.6.jar:24.4.6] at com.vaadin.flow.data.binder.Binder$BindingImpl.execute(Binder.java:1673) ~[flow-data-24.4.6.jar:24.4.6]

Looks like the issue is with setValue which is not making sense to me. Any pointers to help guide me in the right direction?

Thank you

1