Binder setBean and value can not be null


I have a question about Binder, to see if someone can answer it.
I have an Employee object that has some fields that are mandatory and others that are not. When starting the page I set up the binder with its validators, I obtain the employee data from a database and assign it to the bean whose class is declared in the binder and I do the setBean(obj);
If any of the mandatory fields are null or empty, doing the setBean gives me the exception: “value can not be null” and my question is, is it possible to do the setBean and to assign all the fields without that of the exception ? (I do not know if the method I use is the correct one), one thing that occurs to me is to configure the validators after doing the setBean, but I do not know if it’s the right thing to do :slight_smile:

Thanks for everything

Hi Eduardo

In a case like this (when you load a persisted object that already triggers validation error), it is probably better to use readBean(obj); instead of setBean(obj)?, which allows you to do the validation at a time of your choosing (usually at saving/submitting). Be aware that this change might have other implications, for example you can not use binder.getBean() anymore to get the bean from the binder, and your Employee obj is not automatically synchronized with new input values when they change.
If there are other solutions to this problem that would allow you to keep using binder.setBean(obj); then I would be interested in hearing them as well. In my opinion this should not be the reason to decide between setBean and readBean.

Thanks Kaspar, but the solution with readBean doesnt work… Also i have value can not be null.
I’ve tried to set the validatios after set or read bean, and “i have value can not be null”…
So, my solution is not use set or read, do the set “manually” and works …