Question

I have a JS overlay layer which sets up data entry controls on my web app as dojo widgets. I grab inputs like <input type="text" class="date" /> and turn them into DateTextBox widgets.

Here is my basic code:

            var df = dateformat;
            df.replace('mm', 'MM');
            var val = input.value;
            if (val == undefined){
               val = '';
            }
            return new datebox({
                "label": input.title,
                "value": val,
                 "name": input.name,
                   "id": input.id,
          "constraints": { "datePattern": df }
            });

My problem is that all DateTextBox's end up set with '1970-00-01' as the initial date if no value is provided. I would like them to be set to null or undef if no value is provided but this doesn't seem possible until after startup.

Is there a way to set this before I call widget.startup()?

Was it helpful?

Solution

Instead of passing empty string to the DateTextBox you need to pass the literal null or undefined. I put together a jsfiddle demonstrating the different types of values and how they initially display:

require(['dijit/form/DateTextBox', 'dojo/domReady!'], function (DateTextBox) {
    //initial value current date
    new DateTextBox({
        value: new Date()
    }).placeAt('myBody');
    //no value
    new DateTextBox({

    }).placeAt('myBody');
    //null value
    new DateTextBox({
        value: null
    }).placeAt('myBody');
    //undefined value
    new DateTextBox({
        value: undefined
    }).placeAt('myBody');
        //empty string value
    new DateTextBox({
        value: ''
    }).placeAt('myBody');
});

jsfiddle

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top