This project has moved and is read-only. For the latest updates, please go here.

Error working with empty date field

Feb 10, 2012 at 8:00 PM

Hi,

I am using SPUtility.js to customize edit form by making all the fields read-only for specific users. It contains lot of date fields and it is throwing an exception when date field is empty and when i am trying to make that field read only.

Here is the error: 'undefined' is null or not an object.

Basically, issue is if the date is empty it is not able to make it read only or hide.

I changed the source code (highlighted in red below) of sputility.js to acheive this functionality. Here are my changes.

Hope it helps people having same issue.

 

****************************************************************************************************

/*
     *    SPDateTimeFieldValue class
     *    Used to set/get values for SPDateTimeField fields
     */
    SPDateTimeFieldValue = Class.create({
        initialize: function (year, month, day, strHour, strMinute) {
            this.Year = year;
            this.Month = month;
            this.Day = day;
            this.Hour = strHour;
            this.Minute = strMinute;
            this.IsTimeIncluded = !Object.isUndefined(this.Hour) && !Object.isUndefined(this.Minute);
           
            if (this.IsTimeIncluded) {
                if (!this.IsValidHour(this.Hour)) {
                    throw 'Hour parameter is not in the correct format. Needs to be formatted like "1 PM" or "12 AM".';
                }
                if (!this.IsValidMinute(this.Minute)) {
                    throw 'Minute parameter is not in the correct format. Needs to be formatted like "00", "05" or "35".';
                }
            }
        },
       
        /*
         *    SPDateTimeFieldValue Public Methods
         */
       
        IsValidHour: function (h) {
            return !Object.isUndefined(h) && (/^([1-9]|10|11|12) (AM|PM)$/).test(h);
        },
           
        IsValidMinute: function (m) {
            return !Object.isUndefined(m) && (/^([0-5](0|5))$/).test(m);
        },
       
        // returns the part of a date as a string and pads with a 0 if necessary
        PadWithZero: function (d) {
            if (null === d) {
                return '';
            }
            if (typeof d === 'string') {
                d = parseInt(d, 10);
                if (isNaN(d)) {
                    return '';
                }
            }
            if (typeof d === 'number' && d < 10) {
                return '0' + d.toString();
            }
            return d.toString();
        },
       
        // transforms a date object into a string
        GetShortDateString: function () {
            //added by sowjanya
            if(Object.isUndefined(this.Year) || Object.isUndefined(this.Month) || Object.isUndefined(this.Day))
            {
                return ''
            }//added by sowjanya
                       
            var strDate = this.PadWithZero(this.Month) + "/" +
                this.PadWithZero(this.Day) + "/" +
                this.PadWithZero(this.Year);
            return strDate;
        },
       
        toString: function ()
        {   //added by sowjanya
            if(Object.isUndefined(this.Year) || Object.isUndefined(this.Month) || Object.isUndefined(this.Day))
            {
                return ''
            }//added by sowjanya

            var str = this.GetShortDateString(), arrHour;
            if (this.IsValidHour(this.Hour) && this.IsValidMinute(this.Minute)) {
                arrHour = this.Hour.split(' ');
                str += ' ' + arrHour[0] + ':' + this.Minute + arrHour[1];
            }
            return str;
        }
    });

 

****************************************************************************************************

/*
     *    SPDateTimeField class
     *    Date and DateTime fields
     */
    SPDateTimeField = Class.create(SPField, {
        initialize: function ($super, spParams) {
            $super(spParams);
           
            this.DateTextbox = getInputControl(this);
           
            this.HourDropdown = null;
            this.MinuteDropdown = null;
            var timeControls = this.Controls.select('select');
            if (null !== timeControls && 2 === timeControls.length) {
                this.HourDropdown = $(timeControls[0]);
                this.MinuteDropdown = $(timeControls[1]);
            }
        },
       
        /*
         *    SPDateTimeField Public Methods
         *    Overrides SPField class methods.
         */
        GetValue: function () {
            var strHour, strMinute, arrShortDate, strShortDate;
            //strShortDate = this.DateTextbox.getValue();
             
              //added by sowjanya           
            if(this.DateTextbox.getValue() == '')
            { 
                var strYear, strMonth, strDay;
                return new SPDateTimeFieldValue(strYear, strMonth, strDay, strHour, strMinute);
            }
            else
            {
                strShortDate = this.DateTextbox.getValue();
            }//added by sowjanya   
           
           
            if (null !== this.HourDropdown && null !== this.MinuteDropdown) {
                strHour = this.HourDropdown.getValue();
                strMinute = this.MinuteDropdown.getValue();
            }
           
            arrShortDate = strShortDate.split('/');
           
            return new SPDateTimeFieldValue(arrShortDate[2], arrShortDate[0], arrShortDate[1], strHour, strMinute);
        },

 

Feb 11, 2012 at 3:32 AM

Nice! I have a couple other updates I've been meaning to do so I will include this with them. Thanks for posting it!!

Mar 30, 2012 at 4:54 AM

Sorry for the huge delay... got busy. Just wanted to let you know that I updated SPUtility.js to include a fix for making empty date fields read only. I am planning on including this in the 0.9 release after I finish my testing.

If you are eager, you can check out the change here: http://sputility.codeplex.com/SourceControl/changeset/view/c013f52e01d4#SPUtility.js

Mar 30, 2012 at 5:05 PM

Thank You for including this fix in new version. I will check it out.