Validation warnings with an on screen confirmation box

Introduction

Sometimes, customers come up with requirements which make us think, at first glance, … well… euh… that’s not possible with LightSwitch. But often, after deep consideration,  a minute or 2 later, we realize that it is indeed possible with LightSwitch. Moreover, it’s even extremely simple with LightSwitch.

An on screen validation confirmation box is such an example. On entity level (in the domain model), we can specify if a validation error is classified as a warning or an error. What we want in our particular case here is that for a warning we get client side an additional input box, where we confirm that we effectively noticed the warning and that we want to ignore it.

nox

The validation logic in the domain model

Let’s presume we have a validation on the LastName field of our Customer entity. Note that we store the actual validation message in a dedicated ValidationMessage class, which we can could share with the client project.

Our validation logic in this example is very simple: we don’t allow that LastName starts with an ‘x’. In case it does, a warning message is returned.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.LightSwitch;
namespace LightSwitchApplication
{
    public static class ValidationMessages
    {
        public static string NoXInStringMessage = "no x please";
    }

    public partial class Customer
    {
        partial void LastName_Validate(EntityValidationResultsBuilder results)
        {
            if (this.LastName.StartsWith("x"))
            {
                results.AddPropertyResult(ValidationMessages.NoXInStringMessage, ValidationSeverity.Warning);
            }
        }     
    }
}

How do we intercept the validation warning?

Each screen has a _Saving method, where we can ultimately take action before doing the real save. The method has a “handled” parameter which allows us to interrupt the save pipeline. Basically, that’s what we need for our validation warning confirmation box.

In a first step we need to check if the actual validation warning has been returned to the client. This can be done by “Linqing” through the ValidationResults in the details class of the entity.

The final step is to set the handled param based on the decision of the user to accept or ignore the warning.

partial void CustomerDetail_Saving(ref bool handled)
        {
            // when handled is set to true it means that the processing pipeline is discontinued (so no save is done)
            HandleWarningWithConfirmationDuringSave(ref handled);
        }

        private void HandleWarningWithConfirmationDuringSave(ref bool handled)
        {
            var noXWarning = this.Customer.Details.ValidationResults.
                Where(v => v.Severity == ValidationSeverity.Warning &&
                v.Message == ValidationMessages.NoXInStringMessage).FirstOrDefault();

            if (noXWarning != null)
            {
                var message = "Are you sure you want to ignore following warning : " + noXWarning.Message;
                var doSave = this.ShowMessageBox(message, "Validation Warning", MessageBoxOption.YesNo);
                if (doSave == System.Windows.MessageBoxResult.Yes)
                {
                    handled = false;
                }
                else
                {
                    handled = true;
                }
            }
        }

Conclusion

La vita è bella !