The scenario for server side validation in Tea Commerce
Basically I intend to validate step 2 of the cart process, where, in most cases, customer information is entered. My solution must make sure to check whether the data is filled out correctly, whenever the customer tries to go to a step beyond step 2.There must also be taken into account localization of the cart. This means that I do not want to hard code any IDs in my server side code. I also need to check whether there is an order and the order has associated order lines. If the order does not validate, I will send people back to the step that failed validation.
How to do server side validation in Tea Commerce
Server side validation is not standard in Tea Commerce. Luckily the Tea Commerce .NET API gives us plenty of options to do it ourselves. There is undoubtedly lots of ways to solve the problem. However, I have chosen to do it with an Umbraco XSLT library extension, which have the oportunity to redirect the customers to other pages.I will therefore make an Umbraco XSLT library method that can be called from any of the xslt’s. More specifically, it should be called on the steps that come after step 2, and send the customer back if validation fails.
The Umbraco XSLT library extension
There is not much to write about this and I will let the code and the embedded comments speak for themselves. However, I can give you this checklist with the things you must handle before it will work. The code for everything is found a little further down.- Build a dll with the Umbraco library extension and upload it to the bin folder
- Register your XSLT extension in the file /config/xsltExtensions.config
- Make the method call in your xslt’s. Remember that the new extension must be registered in the xslt itself. New xslt’s automatically get your extension inserted into the xsl:stylesheet tag. You can make a new one and copy the entire top of that if you have allready made your cart xslt’s
Full XSLT library extension code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TeaCommerce;
using TeaCommerce.Data;
namespace ServersideValidation {
public class Library {
public Library() {
//This has to be here, because of the way umbraco runs xslt extension
}
public static string ValidateOrder( int currentStepId, int cartStep2Id ) {
try {
//Test to make sure we are not logged in at the back end
var test = HttpContext.Current.CurrentHandler as umbraco.BasePages.UmbracoEnsuredPage;
if ( test == null ) {
//Get the order
Order order = TeaCommerce.Razor.TeaCommerce.GetOrder();
//If there is no order we redirect to step 1 of the cart
if ( order == null || order.OrderLines.Count() == 0 )
HttpContext.Current.Response.Redirect( umbraco.library.NiceUrl( cartStep2Id ) + "?validate=false", true );
//If the current step is not step 1 we validate the data on that step
if ( currentStepId != cartStep2Id ) {
//Get the values we must validate
string firstName = order.GetPropertyValue( "firstName" ),
lastName = order.GetPropertyValue( "lastName" ),
streetAddress = order.GetPropertyValue( "streetAddress" ),
zipCode = order.GetPropertyValue( "zipCode" ),
city = order.GetPropertyValue( "city" ),
email = order.GetPropertyValue( "email" );
//Simple validation of the values
if ( string.IsNullOrEmpty( firstName ) ||
string.IsNullOrEmpty( lastName ) ||
string.IsNullOrEmpty( streetAddress ) ||
string.IsNullOrEmpty( zipCode ) ||
string.IsNullOrEmpty( city ) ||
string.IsNullOrEmpty( email ) ) {
//Go back to step 1 if validation fails
HttpContext.Current.Response.Redirect( umbraco.library.NiceUrl( cartStep2Id ) + "?validate=false", true );
}
}
}
} catch ( Exception ex ) {
return ex.Message;
}
return string.Empty;
}
}
}
The registration in Umbraco’s /config/xsltExtensions.config file
The method call in cartStep03.xslt (And presumably in subsequent steps)
<xsl:template match="/">
<xsl:variable name="step2PageId" select="umbraco.library:GetDictionaryItem('step2PageId')"/>
<xsl:value-of select="ServersideValidation:ValidateOrder($currentPage/@id, $step2PageId)" />
dansk version
Twitter
LinkedIn