Question

I'm looking for a solution to my problem but still haven't found. In my bean I'm using annotations to validations but doesn't work and I'm looking for in all internet to do this work.

I'm using: Vaadin 7 and Maven

I do this.

/** person's bean */
@Entity
public class Person{

@Id
@GeneratedValue
private Integer id;

@NotNull
@NotEmpty
@Size(min=5, max=50, message="insert first name")
private String firstName;

@NotNull
@NotEmpty
@Email private String email;

//get and set
}

//my app
public class LoginView extends VerticalLayout{
    private TextField firstName, email;
    private BeanFieldGroup<Person> binder;
    private FormLayout form;

    public LoginView(){
           form = new FormLayout();
           binder = new BeanFieldGroup<Person>(Person.class);
           Field<?> field = null;
           field = binder.buildAndBind("Firstname", "firstName");
           firstName = (TextField)binder.getField("firstName");
           form.addComponent(firstName);

           field = binder.buildAndBind("Email", "email");
           email = (TextField)binder.getField("email");
           form.addComponent(email);
           this.addComponent(form);
     }
}

How I can do this work ?

enter image description here

/** maven dependecy */
<dependency>
    <groupId>com.vaadin.addon</groupId>
    <artifactId>beanvalidation-addon</artifactId>
    <version>0.6.2</version>
</dependency>



/** exception */
java.lang.NoSuchMethodError: com.vaadin.event.EventRouter.fireEvent(Ljava/util/EventObject;Lcom/vaadin/server/ErrorHandler;)V
    at com.vaadin.server.VaadinService.onVaadinSessionStarted(VaadinService.java:783)
    at com.vaadin.server.VaadinService.createAndRegisterSession(VaadinService.java:740)
    at com.vaadin.server.VaadinService.doFindOrCreateVaadinSession(VaadinService.java:696)
    at com.vaadin.server.VaadinService.findOrCreateVaadinSession(VaadinService.java:637)
    at com.vaadin.server.VaadinService.findVaadinSession(VaadinService.java:501)
    at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1376)
    at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:238)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)
Was it helpful?

Solution

Alright, found the correct implementation. I'm going to show you the code at the end. What you need to do:

  1. Set an actual instance of Person as your data source for the binder
  2. Get rid of the field variable, it's useless in your code, you already have the firstName and email fields which you end up adding to your form layout, field doesn't end up anywhere.
  3. Add a button to submit the form and checkout the error messages if there are any.

Here's the code:

@Theme("mytheme")
public class MyVaadinUI extends UI {

    private Field firstName, email;
    private FormLayout form;

    @Override
    public void init(VaadinRequest request){
        VerticalLayout layout = new VerticalLayout();

        //1. set a propertyitem for the fieldgroup
        final BeanFieldGroup<Person> binder = new BeanFieldGroup<Person>(Person.class);
        binder.setItemDataSource(new BeanItem<Person>(new Person()));
        form = new FormLayout();


        //2. don't use field Field, it's useless
        firstName = binder.buildAndBind("Firstname", "firstName");
        email = binder.buildAndBind("Email", "email");

        form.addComponent(firstName);
        form.addComponent(email);

        layout.addComponent(form);

        //3. add a button to submit the form
        form.addComponent(new Button("OK", new Button.ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
                try {
                    binder.commit();
                    Notification.show("Thanks!");
                } catch (FieldGroup.CommitException e) {
                    Notification.show("You fail!");
                }
            }
        }));
        setContent(layout);
    }
}

Just so you know, I used a couple of different dependencies, but you should try with your own first. Just change the code and see what happens. These are the dependencies I used:

        <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.0-api</artifactId>
        <version>1.0.1.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>5.1.0.Final</version>
    </dependency>

OTHER TIPS

Ok, like I said, you need to use the BeanValidationForm that's provided to you in the Bean Validation addon from Vaadin.

Below is an example:

@Theme("mytheme")
public class MyVaadinUI extends UI {

@Override
public void init(VaadinRequest request){
    VerticalLayout layout = new VerticalLayout();

    Person bean = new Person();

    final BeanValidationForm<Person> form = new BeanValidationForm<Person>(
            Person.class);

    // show null values as empty strings
    form.setFormFieldFactory(new DefaultFieldFactory() {
        @Override
        public Field createField(Item item, Object propertyId,
                                 Component uiContext) {
            Field field = super.createField(item, propertyId, uiContext);
            if (field instanceof TextField) {
                ((TextField) field).setNullRepresentation("");
            }
            return field;
        }
    });

    // use a BeanItem<BeanToValidate> for full validation
    form.setItemDataSource(new BeanItem<Person>(bean));

    // validate immediately when field focus lost but use explicit commit
    form.setImmediate(true);
    form.setWriteThrough(false);

    layout.addComponent(form);

    // explicit validation button
    Button validate = new Button("Validate");
    validate.addListener(new Button.ClickListener() {
        public void buttonClick(ClickEvent event) {
            try {
                form.validate();
                Notification.show("Validation successful");
            } catch (Validator.InvalidValueException e) {
                Notification.show(
                        "Validation failed: " + e.getMessage(),
                        Notification.TYPE_WARNING_MESSAGE);
            }
        }
    });
    layout.addComponent(validate);

    setContent(layout);
}
}

I'm adding this as an answer because I think it will deter you from trying to use the Bean Validation addon altogether.

The problem is that Bean Validation addon is only compatible with Vaadin 6+ versions (I just recently took a look at the addon page to see if its compatible): Vaadin Bean Validation supported Vaadin versions

I'm guesing that since you tagged this question with vaadin7, that you're using Vaadin 7. So you can't use this addon, hence the exception you're getting.

You need to look for another way to provide validation for you beans. One way is to add custom Validator instances to your fields. Check out the Bean Validation section of the Vaadin book page.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top