Spicing up old date input forms using Dojo's DateTextBox widget

I've recently been working a lot w/ Django and have used some of its facilities to automatically generate forms. While that's pretty nifty, I wanted the date fields to be assisted by javascript, more specifically Dojo. I think it's fair to say that most new websites need to have a dropdown date calendar when letting the user select a date. Fortunately, this turns out to be very simple using Dojo.

The general gist of the way I implemented this was to:

  1. Let Django create the HTML form.
  2. Create a function that gets called onLoad and turns specific fields into DateTextBoxes by programmatically creating them.
It's a pretty simple approach and it's worked well for me. But enough chatter, here's the example. First of all, let me define an example form:

<form action='.' method='POST'>
Birth Date: <input type='input' name='birth_date' />
</form>

For obvious reasons I'm going to keep the form short and simple, but just wanted to make sure we're all on the same page (no pun intended). Anyways, here's the javascript code that I use (Warning: assumes that you've already loaded the dojo.js file, please refer to the xhrGet/ xhrPost story for details on how to do that).

function string_to_date (args) {
        args = dojo.string.trim(args);
        arr = args.split('-');
        return new Date (parseInt(arr[0]), parseInt(arr[1], 10) - 1, parseInt(arr[2].substr(0,2), 10));
}

// Add the DateTextBox widget
function init_form () {
        dojo.require('dijit.form.DateTextBox');
        dojo.require('dojo.date');
        dojo.require('dojo.string');
       
        // Birth Day
        var div = document.getElementsByName('birth_date')[0];
        var val = ''
        if (dojo.string.trim(div.value) != '') {
                val = string_to_date (div.value);
        }
        var w = new dijit.form.DateTextBox ({
                name: div.name,
                value: val
        }, div);
}

dojo.addOnLoad (init_form);

The first function in the code is string_to_date. I couldn't find a good Dojo function that simply takes a string (in the 'YYYY-MM-DD' format) and converts it to a Date object, so I hacked that together briefly (got parts of it from old Dojo code). You should do more error-checking in a production environment, but it'll do for this example.

Afterwards, I define the init_form function, which finds the form element, takes its value (if it exists) and converts it to a Date object, and then finally feeds all of the info the the DateTextBox constructor. It's important that you give the constructor the name parameter to make sure your current form-parsing code (in PHP, Python, whatever) still works, since Dojo by default doesn't assign a name to the DateTextBox input field.

And lastly, we tell dojo to call the init_form function once the page has been loaded. And that's it! It's really not a lot of code or complicated logic, so I hope you'll try and spice up ye olde web forms you've got lying around using Dojo.

Comments

string_to_date

instead of string_to_date, try this:

var d = dojo.date.stamp.fromISOString("2008-10-16")

Also, you can create the DateTextBox declaratively like
<input id="datebox" name="birth_date" dojoType="dijit.form.DateTextBox">
and it will just do the conversion for you! No script needed! (it assumes yyyy-mm-dd coming from the server, uses this as a hidden format, but displays something more palatable to the user according to local custom) Only thing is it won't do the trim for you. Either the server will need to format the data properly or you'll have to override the serialize methods on the widget to fix it up.

File Upload with iframe not working in IE

i am trying to upload a file using Dojo iframe with the help of iframe.send

it works fine with FF but ity doesnt work with IE

Can any one help me out in solving this problem

Back to top