datePicker return null when enter a malformed string

I have a simple datePicker field below

<datePicker id="createdDateFromField" property="createdDate"  width="100%"/>

I want to validate in datePicker field when end-users typing something like ‘abc’ to the datePickerField in the view
My expectation: I want to throw an error like ‘wrong format, please re-enter’.
But I cant do this because:

  • createdDateFromField.isEmpty() return true
  • this control dont have any method for me to get the input string from the view
    => I cant validate datePicker field

How can I get the input string from the view

Any solution for this issue

hi @anhnct.work ,
I would suggest not to validate the wrong entry - you should prevent it in the first step.
You can add to the datePicker an allowedCharPattern which will block invalid data entry.
You have to inject your datePicker in the controller and add the pattern, e.g.

    @ViewComponent
    private TypedDatePicker<Comparable> validDateField;

    @Subscribe
    public void onBeforeShow(final BeforeShowEvent event) {
        validDateField.setAllowedCharPattern("[0-9/]");
    }

For details regarding the pattern syntax take a look at
https://docs.jmix.io/jmix/flow-ui/vc/common-attributes.html#allowedCharPattern

BR
Stefan

No, it doesnt help at all because you can type something like //// or 33/44/56789. AllowCharParrtern is not a solution, i think

Sorry that you don’t see the advantage of the AllowedCharPattern.
Important to know is, that this pattern is used to ‘validate’ each individually entered character not the whole text you entered - so the functionality only stops entering totally wrong characters.
But it doesn’t check or prevent to enter an invalid date format.

But back you your question - you will never get the text from the field since the datatype of the field is Date (or similar). If the entered text is not a valid date the value is always null.

If you need these kind of additional checking than you can probably use ClientValidatedEvent which
gives you some kind of information, e.g. isValid(), fromClient, …
You have to create some logic to notify the user and clean the field, it’s up to you.

BR
Stefan

1 Like