Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    TimeDateItem Ignoring setEnforceDate

    I am creating a DateTimeItem, setting a default value and then setting "setEnforceDate(true)" on it.

    It seems to ignore this last call since it still allows me to type anything into the text box for the DateTimeItem and never gives an error whether I click out of the box, hit enter or anything else.

    Am I missing a step?

    I am using SmartGWT Power 2.5 official release.

    Thanks you for any help on this!

    #2
    This should be working.
    Here's a simple test case that works for us:
    Code:
    public class EnforceDateTest implements EntryPoint  {
    
        @Override
        public void onModuleLoad() {
            DynamicForm testForm = new DynamicForm();
            DateTimeItem testItem = new DateTimeItem("time");
            testItem.setEnforceDate(true);
            
            testForm.setItems(testItem);
            testForm.draw();
            
            
        }
    
    }
    In this example if you enter some bogus string like "asdf" and tab out of the field an error is displayed. If you enter a valid date or datetime string ("1/1/2001", for example) and tab out it is correctly parsed to a date-time value and the error disappears.

    Comment


      #3
      Thank you for the quick response.

      If I run the code you placed below in a sample project it works properly, but if I run it in my project it does not seem to work (no error icon is displayed). I will look into it further and post something if I can figure out what is going on.

      Thanks again.

      Comment


        #4
        So I figured out the difference between the project where setEnforceDate works and when it is ignored.

        In the project where it is failing we are using a specific DateTimeFormat ("MM/dd/yyyy HH:mm:ss:SSS".

        This is done through the suggested approach of using the DateUtil class to set this as the format for the DateTimeDisplayFormatter and is used in the DateParser.

        I would appreciate it if you guys could verify this on your end and let me know if you see a way around this.

        Thank you in advance.

        Comment


          #5
          Another issue that I am seeing that may be related...

          In the same project I described above with the custom datetime formatter when I call setUseMask on the DateTimeItem it ignores the custom datetime formatting and uses a mask that looks like the default formatting.


          Here is the code I am using to setup the custom format (which seems to work in all cases outside of the two I have described here):

          Code:
             private static final String DATE_TIME_FORMAT_STRING = "MM/dd/yyyy HH:mm:ss:SSS";
             private final DateTimeFormat dateFormat = DateTimeFormat.getFormat(DATE_TIME_FORMAT_STRING);
             private final DateDisplayFormatter dateFormatter = new DateDisplayFormatter()
             {
                @Override
                public String format(Date date)
                {
                   if(date == null)
                   {
                      return null;
                   }
                   else
                   {
                      return dateFormat.format(date);
                   }
                }
             };
          
          .........
          
                DateUtil.setShortDatetimeDisplayFormatter(dateFormatter);
                
                DateUtil.setNormalDateDisplayFormatter(dateFormatter);
                DateUtil.setShortDateDisplayFormatter(dateFormatter);
                DateUtil.setDateInputFormat(DATE_TIME_FORMAT_STRING);
                DateUtil.setDateParser(new DateParser()
                {
                   
                   @Override
                   public Date parse(String dateString)
                   {
                      Date date = dateFormat.parse(dateString);
                      return date;
                   }
                });

          Thanks in advance for any help on this.

          Comment


            #6
            We're looking into the first issue (failure of "enforceDate" to raise a warning with this custom formatter).

            On the second issue - this is a known limitation of the "mask" functionality for DateItems and DateTimeItems. Fundamentally for the mask to work it needs to know the expected format of the date string. This is known for the standard DateDisplayFormat enum but for an entirely custom formatter / parser function pair the item has no way of knowing what format will be accepted.

            Comment


              #7
              The first issue seems to just be an implementation issue.
              The dateFormat.parse(...) call will throw an exception if passed an invalid (non-convertable) string. Therefore you need a try...catch block around it (and to return null to indicate the value could not be parsed to a date).
              Something like this:

              Code:
                         public Date parse(String dateString)
                         {
                             Date date;
                             try {
                                 date = dateFormat.parse(dateString);
                             } catch (Exception e) {
                                 date = null;
                             }
                            return date;
                         }
              When this method returns null, the form item should display the error icon.

              Comment

              Working...
              X