Search code examples
vaadinvaadin8vaadin-gridvaadin-flow

why do we need to set bean **binder.setBean(object)** in binding in vaadin


This is the binding of all form fields. I am having long type of textfield so it cannot be null.

consumerBinder.forField(legal_hold)
    .bind(Consumer::getLegal_hold, Consumer::setLegal_hold);

consumerBinder.forField(deceased_fg)
    .bind(Consumer::getDeceased_fg, Consumer::setDeceased_fg);

consumerBinder.forField(household_id)
    .withConverter(new StringToLongConverter("Must be a number"))
    .bind(Consumer::getHousehold_id,Consumer::setHousehold_id);

consumerBinder.forField(ins_efid)
    .withConverter(new StringToLongConverter("Must be a number"))
    .bind(Consumer::getIns_efid,Consumer::setIns_efid);

consumerBinder.forField(ins_pqid)
    .withConverter(new StringToLongConverter("Must be a number"))
    .bind(Consumer::getIns_pqid,Consumer::setIns_pqid);

consumerBinder.forField(ins_rid)
    .withConverter(new StringToLongConverter("Must be a number"))
    .bind(Consumer::getIns_rid,Consumer::setIns_rid);

consumerBinder.forField(upd_efid)
    .withConverter(new StringToLongConverter("Must be a number"))
    .bind(Consumer::getUpd_efid,Consumer::setUpd_efid);

consumerBinder.forField(upd_pqid)
    .withConverter(new StringToLongConverter("Must be a number"))
    .bind(Consumer::getUpd_pqid,Consumer::setUpd_pqid);

consumerBinder.forField(upd_rid)
    .withConverter(new StringToLongConverter("Must be a number"))
    .bind(Consumer::getUpd_rid,Consumer::setUpd_rid);

consumerBinder.forField(upd_tmstmp)
    .withConverter(new StringTimestampConvertor())
    .bind(Consumer::getUpd_tmstmp, Consumer::setUpd_tmstmp);

consumerBinder.forField(ins_tmstmp)
    .withConverter(new StringTimestampConvertor())
    .bind(Consumer::getIns_tmstmp, Consumer::setIns_tmstmp);

consumerBinder.forField(deceased_dt)
    .withConverter(new StringTimestampConvertor())
    .bind(Consumer::getDeceased_dt, Consumer::setDeceased_dt);

binder.setBean(new Consumer());

Because of this i am getting default value 0 for long fields. But i want the form to be clear.


Solution

  • I can come up with two different options for you depending on exactly how you want the application to behave.

    If you want to always show 0 as empty in the text fields, then you can create a custom StringToLongConverter subclass that converts 0 to "" and otherwise delegates to the original implementation.

    If you want 0 to show as empty only when entering new data but show 0 as 0 when editing an existing item, then you can skip setBean when initializing the binder and instead create and populate a bean instance only when saving, e.g. something like this:

    saveButton.addClickListener(event -> {
      Consumer newConsumer = new Consumer();
      binder.writeBean(newConsumer);
      saveInDatabase(newConsumer);
    });
    

    You can read the documentation at https://vaadin.com/docs/v14/flow/binding-data/tutorial-flow-components-binder-load.html for more information about the difference between setBean and writeBean.