• Microsoft LightSwitch – Using the Entity Field Custom Validation

    Posted Aug 31st, 2010 By in LightSwitch With| 1 Comment | Microsoft LightSwitch – Using the Entity Field Custom Validation
    Share on TwitterSubmit to StumbleUponSave on DeliciousDigg ThisSubmit to reddit

    In my last post I demonstrated how to use the LightSwitch Is Computed field property. In this post I am going to extend the validation of a field by doing some Custom Validation.

    The scenario is this; I want to schedule the times to go and customers. Be it for customer sales call or some training, whatever. I want to build some scheduling into my application to help better manage my time.

    Here is a look at my Customer table that I created earlier:

    The Customer Table

    The Customer Table

    I am going to do is add a new field to my Customer entity. The new field is going to be a DateTime field, rather than just a Date type. I want to keep track of the date and time that I am going to schedule.

    Adding the DateTime Field to my Customer Entity

    Adding the DateTime Field to my Customer Entity

    Now, with my new SheduledVisitDateTime field added to my Customer entity, I am going to add it to a screen so that I can start adding some scheduling.

    I already have a List and Details type screen that I created earlier. I open the CustomerList screen designer.

    Over in the left had side of the designer I see the listing for the CustomersCollection that my screen is using. In the list I see my new ScheduledVisitDateTime field I created.

    New field showing in screen designer

    New field showing in screen designer

    Because I created this screen with an earlier version of my Customer table, the new ScheduledVisitDateTime field does not automatically get added to the screen. So I drag and drop the ScheduledVisitDateTime field to the vertical stack used for the CustomerDetails section of my screen. I drop the field underneath the existing Last Contact Date field in the tree.

    Adding the field to the screen (via the designer)

    Adding the field to the screen (via the designer)

    Super! Now I hit the F5 key to start the application in debug mode…

    Hmm. Interesting. The application starts, as expected, and I can now see the ScheduledVisitDateTime field on my screen. Here is what I see…

    The new field on the screen

    The new field on the screen

    There are three things that I see that I have to “tweak”. The first is the silly looking label. The second is that the label is bold, which suggests that the field is configured with the Is Required property set to true. The third thing I notice is the stupid looking date and time that defaults for the field.

    “1/1/1900 12:00AM’ is not a date that I want to default in there. In fact, I want the field to default with nothing in it. I want the option to either have a date, or none at all. I think what happened was that because the Is Required property was set to true when I first added the field, LightSwitch may have added some default dates to all my existing records. I am not totally sure that this was the case however something in my head is telling me this is what happened.

    So back to the table designer I go.

    In the Customer table designer I give the ScheduledVisitDateTime field better Display Name and Description property values. I also update the Is Required property so that it isn’t checked. This will give me the option to save a customer without having a date and time value in my new field.

    Updated Field Properties

    Updated Field Properties

    I’m pretty sure that I know a little something about setting LightSwitch field properties now, so I fire up the application again by hitting the F5 key, just to prove that little horned fella, let’s name him Nelson for now, on my shoulder wrong…

    …Nelson laughs.

    All looks good in the running application, except for those nasty defaults again. Not sure why those show like that. Just as a test, I added and saved a new customer, but didn’t add any value for my new field. Interestingly, the record saved with out a value, and when I view the new customer via my CustomerList screen, the new field contains an empty value.

    No worries. I delete the ScheduledVistitDateTime value for all my customers – to start from a new slate.

    Moving on…

    With my new field, I now want to add some simple validation for any data that I enter in it. The validation, or business rule is that I want to only allow a date and time that is in the future. It would be silly to add a past date into a place where I want to store dates for the future.

    “What kind of scheduling is that!?! Dumb Ass! Haw Haw” …Nelson exclaims.

    Back to the Customer table designer in LightSwitch.

    With the ScheduledVisitDateTime field selected, I head over to the Properties panel. At the bottom of the Properties panel is a link titled Custom Validation. I click it.

    The Custom Validation link

    The Custom Validation link

    Clicking the Custom Validation link opens the code designer for my Customer entity. Specifically, the code designer opens with a procedure stub already created and titled ScheduledVisitDateTime_Validate()…

    Private Sub ScheduledVisitDateTime_Validate(ByVal results As EntityValidationResultsBuilder)
       ' results.AddPropertyError("")
    
    End Sub

    This is where I am going to add some logic that will validate that the date and time I enter into the ScheduledVisitDateTime field are in the future. So, that’s what I do…

    Private Sub ScheduledVisitDateTime_Validate(ByVal results As EntityValidationResultsBuilder)
    
          ' If the entered value is not in the future, then throw the error.
          If Me.ScheduledVisitDateTime < DateTime.Now() Then
            results.AddPropertyError("The scheduled date and time must be in the future.")
          End If
    
        End Sub

    So I run the application by hitting the F5 key.

    KaBLAMMO. {Insert Roger Daltrey “yeeeaaaahhhh…..” Won’t Get Fooled Again screem here}

    …Nelson has fallen off my shoulder. Not sure where he went to.

    Meanwhile, back at the LightSwitch ranch…the application launches and displays my CustomerList screen. I select the first customer recordI purposely click the labelled Next Scheduled Visit date and select a date in the past.

    Validation errors showing on screen.

    Validation errors showing on screen.

    Looks like my custom validation is working! LightSwitch shows both a notification in the tab for the CustomerList screen, as well as shows a red border around my new field. Clicking on the in the message in the tab brings up the error message that I defined in my validation code…

    The tab for the screen showing the error message.

    The tab for the screen showing the error message.

    Hovering over the tooltip corner of red border error indicator of the field causes LightSwitch to display the same error message as a tooltip…

    Same error message as a ToolTip.

    Same error message as a ToolTip.

    Cool!

    Now, I update the value to a future datetime value and presto, the error messages go away. I save the record. I now am able to better manage my time by scheduling my next visit with my customers.

    In my next post, I am going to extend this scheduling stuff by adding a new table with a relationship with this one. Doing so will let me add more granular scheduling, as well as set me up to better visualize my schedule by using queries.

    Stay tuned!


    • delicious
    • digg
    • reddit

    Paul
    My name is Paul Patterson and I am a software developer who has a keen interest in technology, including; open source, .Net, and anything Interweb. When not crafting some code, I can be found learning something new about photography. As well, I occasionally escape to the "music room" with my guitars to practice a few scales and then jam with my favourite FM radio stations.

Leave a Reply


Comments (1)

Reply
Tweets that mention Microsoft LightSwitch – Using the Entity Field Custom Validation - Wrench in the Cog -- Topsy.com » 01. Sep, 2010

[...] This post was mentioned on Twitter by PaulPatterson and Dominik, Dominik. Dominik said: RT @PaulPatterson: Just posted an experiment in using custom validation on a field in #LightSwitch. http://bit.ly/c2fQeg Why? Because I can. Mwaahaha. [...]

© Copyright Paul S Patterson - Please, no touchie. :)