August 2010 - Posts - Hilary Stoupa
in

InfoPath Dev

Hilary Stoupa

August 2010 - Posts

  • Looks Can Be Deceiving -- Dates, Date Times, and "T"

    One of the many fantastic things about InfoPath forms is that we can dictate a data type for a field. If we set a field to be a decimal, for example, the poor user determined to enter an 'a' will always be thwarted.

    But if you don't understand what the data for a data type really looks like, it is pretty easy to find yourself in a mess of validation errors. You know better than to set a decimal field to 'a', of course, but what about a date? What should a date look like if you want to set an InfoPath date field? What happens if you try to set a date field to the value of the function now()? Why is the letter "T" even in the title of this post?

    All gripping questions that we are now going explore.

    First, don't be fooled by form controls. Given the following data structure:

    We can add date pickers to our form for any of those data types:

    And, I bet if I asked you right now what the data looked like, you'd say "Month, slash, Day, slash, Year". If I asked you to set a rule to set that field's value, you'd use "9/8/2010", for example, and feel very pleased until you saw the data validation errors:

    Only the date picker bound to the text field doesn't care about the value. But that control and the field it is bound to are not very discerning. You can type whatever you want in there:

    Why is InfoPath being mean to me? If I save the XML from my original image, where I had no validation errors: 

    Try to look past the xsi namespace declaration and focus on the values:

    Now, let's look at the XML from the sample with the validation errors (this time I'll exclude the namespace declarations):

    Especially interesting is the dateTime field. Even when we don't have a time filled out, that datatype requires "T00:00:00".

    No matter how we format our control to display, the date and datetime date types will have a specific format. "yyyy-MM-dd" for date, "yyyy-MM-ddTHH:mm:ss" for datetime. And if you set a datetime to the today() function value (which returns a date) or a date to the now() function value (which returns a datetime), you will have validation errors. If you are uncertain why you have an error, you can always save the XML and take a look in a text editor. Select a value using a date picker, and compare to the value you are trying to set the field to.

    You can also change your control to show the XML value of the field. Right click the control and open its properties, then click the Format button:

    Select the top option:

    Preview and select some dates:

    Keep this in mind when you are copying data into your form fields from other data sources as well. A SharePoint list may have a field that looks like a date, but is actually a datetime.... and missing the "T" separator between the date and the time! Copy that into a date or datetime field in an InfoPath form and it will behave as if you have surgically removed its birthday.

    So remember -- with form controls and data types, you may not be seeing what you are getting at all. If you are troubleshooting a validation error, look at the XML data, make sure the controls you are debugging with are showing you the actual data, and you'll be able to make everything work nicely in no time.

Copyright © 2003-2017 Qdabra Software. All rights reserved.
View our Terms of Use.