[2-0 Java] convert empty strings to null values

1,048 views
Skip to first unread message

Ronald Haring

unread,
Jul 29, 2012, 5:09:54 AM7/29/12
to play-fr...@googlegroups.com
Hello all,

I would like to register a custom editor so that each empty string will be converted to a null value in my model. I tried registering a formatter in the global java file, but when I put a breakpoint on this formatter I see that this is only called if the content of the property is filled. How can I register a custom editor so that an empty value will be nullified?

This is what I have now:
public void onStart(Application app) {
        Formatters.register(String.class, new Formatters.SimpleFormatter<String>() {
            @Override
            public String parse(String text, Locale locale) throws ParseException {
                text = text.trim();
                if (text.isEmpty()) {
                    text = null;
                }
                return text;
            }

            @Override
            public String print(String s, Locale locale) {
                return s.trim();
            }
        });
    }

Regards
Ronald
Reply all
Reply to author
Forward
0 new messages