Uploaded image for project: 'ZK'
  1. ZK
  2. ZK-4346

datebox constraint parsing errors

XMLWordPrintable

    • ZK 8.6.3 S1

      Steps to Reproduce

      using 5 digit years for databox constrains, causes varying inconsistent results/errors

      1) year 10000

      <datebox format="yyyy-MM-dd" constraint="before 100000110"/> 
      

      2) year 10001

      <datebox format="yyyy-MM-dd" constraint="before 100010110"/>
      

      3) year 10002

      <datebox format="yyyy-MM-dd" constraint="before 100020110"/>
      

       
      use the 3 scenarios above and type a valid date e.g. 2019-01-01 then tab out (blur)

      Actual Result

      1) constraint violation => "Out of range: <= 0999-12-11"
      2) constraint violation => "Out of range: <= 1000-10-11"
      3) JS error in console:

      parseConstraint_ @ zul.inp.wpd:1894
      _init @ zul.inp.wpd:1589
      $init @ zul.inp.wpd:1503
      $supers @ zk.wpd:11786
      $super @ zk.wpd:11712
      $init @ zul.inp.wpd:1869
      init @ zk.wpd:10032
      constraint @ zul.db.wpd:2994
      (anonymous) @ zk.wpd:10071
      _set2 @ zk.wpd:11067
      setter.func @ zk.wpd:19461
      zk.Widget.zk.$extends.set @ zk.wpd:19489
      create @ zk.wpd:24902
      create @ zk.wpd:24914
      mtBL @ zk.wpd:24721
      doEnd @ zk.wpd:24198
      _zkf @ zk.wpd:24238
      (anonymous) @ zul.db.wpd:3793
      doEnd @ zk.wpd:24198
      _zkf @ zk.wpd:24237
      (anonymous) @ zul.inp.wpd:5376
      doEnd @ zk.wpd:24198
      _zkf @ zk.wpd:24237
      (anonymous) @ zk.fmt.wpd:549
      doEnd @ zk.wpd:24198
      _zkf @ zk.wpd:24237
      (anonymous) @ zk.fmt.wpd:549
      

      Expected Result

      A) correct datebox constraint parsing and constraint error messages
      B) meaningful error message if constraint limits are exceeded (if A is not possible)

      Debug Info

      Root Cause

      the parsing issues/errors happen because of the default hard-coded constraint date format yyyyMMdd which doesn't support 5 digit years, due to missing separators

      https://github.com/zkoss/zk/blob/1e82bea329ea6d627aafb435ed192e1afb85c7e5/zul/src/archive/web/js/zul/inp/SimpleDateConstraint.js#L32
      https://github.com/zkoss/zk/blob/4e84988ae7be6cc7c35b9dad3f0eda93f6c75add/zul/src/archive/web/js/zul/db/Calendar.js#L552

      the server side constraint also uses a hard coded constraint without delimiters
      https://github.com/zkoss/zk/blob/v8.6.2/zul/src/org/zkoss/zul/SimpleDateConstraint.java#L196

      Workaround

      if you need a 5 digit year format for constraint, you can try this.

      <script><![CDATA[
       var fmt = 'yyyyyMMdd';
       zk.afterLoad('zul.inp', function () {
       var _xSimpleDateConstraint = {};
       zk.override(zul.inp.SimpleDateConstraint.prototype, _xSimpleDateConstraint, {
       format: fmt // patch
       });
       });
       zk.afterLoad('zul.db', function () {
       function _getTimeZone(wgt) {
       var parent = wgt.parent,
       tz = parent && parent.getTimeZone && parent.getTimeZone();
       return tz ? tz : wgt._defaultTzone;
       };
       var _xCalendar = {};
       zk.override(zul.db.Calendar.prototype, _xCalendar, {
       _fixConstraint: function () {
       var constraint = this._constraint || '';
       if (typeof this._constraint != 'string') return;
       // B50-ZK-591: Datebox constraint combination yyyymmdd and
       // no empty cause javascript error in zksandbox
       var constraints = constraint.split(','),
       format = fmt, // patch
       len = format.length + 1,
       tz = _getTimeZone(this);
       for (var i = 0; i < constraints.length; i++) {
       constraint = jq.trim(constraints[i]); //Bug ZK-1718: should trim whitespace
       if (constraint.startsWith('between')) {
       var j = constraint.indexOf('and', 7);
       if (j < 0 && zk.debugJS)
       zk.error('Unknown constraint: ' + constraint);
       this._beg = new zk.fmt.Calendar(null, this._localizedSymbols).parseDate(constraint.substring(7, j), format, null, null, null, tz);
       this._end = new zk.fmt.Calendar(null, this._localizedSymbols).parseDate(constraint.substring(j + 3, j + 3 + len), format, null, null, null, tz);
       if (this._beg.getTime() > this._end.getTime()) {
       var d = this._beg;
       this._beg = this._end;
       this._end = d;
       }
       this._beg.setHours(0, 0, 0, 0);
       this._end.setHours(0, 0, 0, 0);
       } else if (constraint.startsWith('before_') || constraint.startsWith('after_')) {
       continue; //Constraint start with 'before_' and 'after_' means errorbox position, skip it
       } else if (constraint.startsWith('before')) {
       this._end = new zk.fmt.Calendar(null, this._localizedSymbols).parseDate(constraint.substring(6, 6 + len), format, null, null, null, tz);
       this._end.setHours(0, 0, 0, 0);
       } else if (constraint.startsWith('after')) {
       this._beg = new zk.fmt.Calendar(null, this._localizedSymbols).parseDate(constraint.substring(5, 5 + len), format, null, null, null, tz);
       this._beg.setHours(0, 0, 0, 0);
       }
       }
       }
       });
       });
      ]]></script>
      

            Leon03 Leon03
            cor3000 cor3000
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:

                Estimated:
                Original Estimate - 2 days
                2d
                Remaining:
                Time Spent - 1 day Remaining Estimate - 1 day
                1d
                Logged:
                Time Spent - 1 day Remaining Estimate - 1 day
                1d