How to validate fields without a bean.

One of my frustration with Vaadin 8 was the need to have a bean to use binder, or having to call validate() that was tricky. Moving to V 10/Flow I decided to fins a better way to input few fields without the need to create a specific bean. For example, so I created a DinaBinder that binds fields to a generic map class that can be used like this:

   // 0.Here are few fields
  private TextField email = new TextField();
  private PasswordField password = new PasswordField();
  private Checkbox rememberMe = new Checkbox("Remember me");
   

    // 1. Create a dynamic binder
    DynaBinder binder = new DynaBinder();
	
	// 2. Bind some fields to it. The bind method receives a component, optionally an ID and
	// an optional lambda acting on BindingBuilder before binding. Returns the component itself
	// so that it can be added to the form in a single statement
    form.addFormItem(binder.bind(email,
            bb -> {
              bb.asRequired().withValidator(new EmailValidator("Bad email"));
            }), "Email");

    form.addFormItem(binder.bind(password, bb -> {
              bb.asRequired();
            }), "Password");
			
    form.addFormItem(binder.bind(rememberMe), "");
	
	.....
	
	
      if (!binder.read().isPresent()) // binder read() returns a map if and only if data is OK, on error is empty
        return;
      
	  // Access field content directly from fields, otherwise you shall read the returned map (no need to ID field)
	  // but shall anyway cast its keyed values. Reading field is simpler.
	  
	  EndUser user = endUserDAO.findByEmail(email.getValue());
	  ...
	  
  


	.....

Well, here is the code for whoever wish to use it.

17237045.java (3.53 KB)
17237048.java (765 Bytes)

This looks interesting, I strongly recommend you to publish it in our [Directory]
(https://vaadin.com/directory).