vaadin / vaadin/framework

DateField with custom empty value and placeholder

Open
#11,359 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

question Stale
Dominant language
Java
Stars
1.8k
Forks
717
Avg merge
2d 6h
Merged PRs (30d)
3

Description

Hi!

I have a problem with DateField using custom empty value. In our system we never store dates as null, instead we use the sentinel date "1900-01-01" to represent an absent/unspecified/empty date.

The docs of HasValue#getEmptyValue() implies that one should be able to redefine what is considered empty:

LocalDate emptyDate = LocalDate.parse("1900-01-01");

DateField field = new DateField()
{
    @Override
    public boolean isEmpty()
    {
        return getValue() == null || getValue().equals(emptyDate);
    }

    @Override
    public LocalDate getEmptyValue()
    {
        return emptyDate;
    }
};
field.setPlaceholder("yyyy-MM-dd");
field.setValue(emptyDate); // Shows "1900-01-01" instead of placeholder
field.setValue(null); // Shows placeholder "yyyy-MM-dd"

I expect to see placeholder when value is equal to empty value, but it only works when value is null. Docs of DateField#setPlaceholder states "Sets the placeholder text. The placeholder is text that is displayed when the field would otherwise be empty, to prompt the user for input." so it seems this does not work as one would expect?

Overriding #formatDate(LocalDate) doesn't work either:

@Override
protected String formatDate(LocalDate value)
{
    if (isEmpty())
    {
        return getPlaceholder();
    }
    else
    {
        return super.formatDate(value);
    }
}

Using Vaadin 8.6.1

Thanks!
// Martin

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by examining DateField#setPlaceholder, HasValue#getEmptyValue(), and the DateField formatting path described in the report. Reproduce the example with the 1900-01-01 sentinel and with null, then trace why only null displays the placeholder. Done means the placeholder appears for both values treated as empty without changing normal date formatting.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.