jQuery-Validation-Engine icon indicating copy to clipboard operation
jQuery-Validation-Engine copied to clipboard

Make the validation engine compatible with UI datepicker

Open posabsolute opened this issue 15 years ago • 6 comments

Currently the blur event get in conflict with the jquery UI action making the validation of date pretty weird

posabsolute avatar Apr 03 '11 19:04 posabsolute

The ValidationEngine with a datepicker

Using a datepicker with the engine is problematic because the validation is binded to the blur event. since we lose the focus before any data is entered in the field it creates a weird bug. Fortunately we implemented a fix where we use a delay when a datepicker is binded.

To use this mode you need to add the class datepicker to your input, like this:

<input type="text" id="req" name="req" class="validate[required] text-input datepicker" value="">

posabsolute avatar May 30 '11 17:05 posabsolute

The validation message appears when I click on any button of the datepicker's calendar.

I Think a better solution for this issue is to register listener for the datepicker events, maybe something like this: $('.datepicker').datepicker({ onSelect: function(dateText, inst) { /* validation here */ } });

http://stackoverflow.com/questions/1814292/jqueryui-datepicker-fires-inputs-blur-before-passing-date-avoid-workaround http://stackoverflow.com/questions/2607397/jquery-validationengine-plugin-custom-callback-for-datepicker-input-fields

emont01 avatar Aug 03 '11 17:08 emont01

Thanks, not a simple issue, I will see how I can ameliorate the datepicker compatibility

posabsolute avatar Aug 03 '11 17:08 posabsolute

my solution for now is comment out the bind and live code in validationEngine.js file and do the validation onClose event of datepicker and it works fine for me. jquery.ValidationEngine.js

        if (!options.binded) {
                if (options.bindMethod == "bind"){

                    ...

                    //form.find("[class*=validate][class*=datepicker]").bind(options.validationEventTrigger,{"delay": 300}, methods._onFieldEvent);

                    ...
                } else if (options.bindMethod == "live") {
                    ...

                    //form.find("[class*=validate][class*=datepicker]").live(options.validationEventTrigger,{"delay": 300}, methods._onFieldEvent);

                    ...
                }

            options.binded = true;
        }

my datepicker:

  $("#inputDate").datepicker(
  {
    beforeShow: function (input) { $('#form1').validationEngine('hide'); },
    onClose: function (dateText, inst)
      { 
        $('#form1').validationEngine('validateField', '#inputDate') 
      } 
  });

emont01 avatar Aug 03 '11 18:08 emont01

Adding a "datepicker" class is not necessary, jQuery UI does all the required DOM manipulation for us. I played around a bit, but I did not find a solution that fits all needs. Maybe this snipped helps anyway:

        /**
         * Is the current field a datepicker, and is it open
         * 
         * @param {jqObject}
         *            field
         * @return true if field has a open datepicker
         */
        _hasOpenDatepicker: function(field) {
            if (!field.hasClass('hasDatepicker'))
                return false;

            if (jQuery('#ui-datepicker-div').css('display') != 'block')
                return false;
            
            return true;
        },

As soon as jQuery UI supports a binding of "onClose" to the function stack of a initialised object I could offer a working solution.

co-operation avatar Aug 16 '11 22:08 co-operation

My solution that helped me: $("#date").datepicker( { beforeShow: function () { $("#date").removeAttr("data-validation-engine"); } });

mandrivnyk avatar Aug 26 '15 09:08 mandrivnyk