danielhanold / danielhanold.pickerwidget

Titanium widget that implements single column, date, and age-range pickers for iOS and Android
39 stars 13 forks source link

cancel button throws error; minDate fails #1

Closed mjstelly closed 10 years ago

mjstelly commented 10 years ago

Daniel, Great job on the widget. Works beautifully except for the cancel button and the minDate assignment. Very possible I'm doing something wrong.

First, the cancel button: When selecting it, I get an error.

[ERROR] :      message = "'null' is not an object (evaluating 'e.data.date')";

(e.data.date is defined in the onDone callback) At first, I thought this occurred when I simply opened and closed the picker without selecting a value. Not true. The error occurred regardless.

Next is the minDate variable. I use moment.js to calculate the value

    var minDate = moment().subtract('years', 2);

which returns the correct value

[INFO] :   [balances] minDate = "2012-08-15T16:18:59.332Z"

I have minDate defined in pickerParams:

        pickerParams : {
            minDate : minDate,
            maxDate : maxDate,
            value : defaultDate,
            maxSelectedDate : maxSelectedDate,
            maxSelectedDateErrorMessage : 'Cannot select future dates.'
        },

However, the user is able to select a date older than the minimum. I am expecting minDate to disallow selections older than this date, which is the converse of maxDate where the user cannot select any date forward of 'maxDate`.

I would appreciate your assistance on how to resolve these issues. Thanks.

danielhanold commented 10 years ago

Hi there and apologies for the late response. I hope I can help you out with your two issues.

Cancel Action

The error message you receive when canceling the date entry is caused because the user didn't actually select a date. Here's an example of the full object returned when a user cancels:

{"cancel":true,"type":"date-picker","id":"birthdate","data":null}

As opposed to the full object when a user selects a date:

{"type":"date-picker","data":{"date":"1900-08-28T05:00:00.000Z","age":113,"unixMilliseconds":-2188321200000,"unixSeconds":-2188321200},"cancel":false,"id":"birthdate"}

You can see that the value of the "data" property is null. If you are treating null as an object and try to access a property that doesn't exist, you get the error message you're seeing. You should check for the "cancel" property in the result object to see if the user canceled or not.

Min Date Issue

I was successfully replicating this, and there was a mistake in my documentation and my own code. The problem is that Titanium ignores the property when maxDate is less than minDate. In the example I gave in the documentation, I am using this snippet:

var today = new Date();
var minDate = new Date(today.setYear(1900));
var maxDate = new Date(today.setYear(today.getYear()-18));

The problem here is that I'm using the same "today" date variable for both minDate and maxDate, which is a logic error. The correct code should look like this:

// Set minimum date to 1900.
var minDate = new Date(new Date().setYear(1900));
// Set minimum date to today - 18 years.
var maxDate = new Date(new Date().setYear(new Date().getYear()-18));

After I made this adjustment in the code, the widget worked correct.