Usage is documented within the code. Please note the following:
- I changed the name of the required fields list variable (from requiredFieldsList to requiredFields) from that in the original link above so it would be consistent with the regexFields variable.
- I made all text fields required by default since that matches my customer's products best.
- You can use html in the validation message. For my customer, they can't enter two words in a text field unless they order a different product. So the validation message says "To enter two words, please order the <a href="Double-Word-Puzzle-P383.aspx">Double Word Puzzle</a>" and provides a link to the correct product.
In ProductHelper.cs BuildProductChoices,
Code: Select all
using System.Collections;
// SG Customization - ZLA 2009-05-20 - Validate custom fields using Regular Expressions
//
// Product Template Usage:
// Customer Field : Text Box named 'Middle Name'
// Merchant Field 1 : Text Box named 'Regex2 Field', value of 'Middle Name'
// Merchant Field 2 : Text Box named 'Regex2 Expression', value of '^\w+$'
// Merchant Field 3 : Text Box named 'Regex2 Message', value of 'Middle name can not include spaces.'
// - Note that Regex2 above can be anything that begins with Regex.
// - For example, 'Regex', 'Regex1', 'RegexA', 'RegexCat', 'RegexDog', etc.
struct RegexFieldValidator
{
public string RegexField;
public string RegularExpression;
public string ErrorMessage;
public RegexFieldValidator(string regexField, string regularExpression, string errorMessage)
{
this.RegexField = regexField;
this.RegularExpression = regularExpression;
this.ErrorMessage = errorMessage;
}
}
// SG Customization - ZLA 2009-05-20 - Validate all Text Boxes / Text Areas unless individual custom fields selected.
//
// Product Template Usage:
// -- Scenario 1 --
// Merchant Field : none
// Customer Field 1 : Text Box named 'First Name' - Required By default
// Customer Field 2 : Text Box named 'Last Name' - Required By default
//
// -- Scenario 2 --
// Merchant Field 1 : Text Box named 'Required Fields List', value of 'First Name,LastName'
// Customer Field 1 : Text Box named 'First Name' - Required
// Customer Field 2 : Text Box named 'Middle Initial' - Optional
// Customer Field 3 : Text Box named 'Last Name' - Required
//
// -- Scenario 3 --
// Merchant Field 1 : Text Box named 'Required Fields List', value of '' (empty string)
// Customer Field 1 : Text Box named 'First Name' - Optional
// Customer Field 2 : Text Box named 'Middle Initial' - Optional
// Customer Field 3 : Text Box named 'Last Name' - Optional
// - Note that 'Required Fields List' or 'RequiredFieldsList' will work. Match based on Name.replace(' ','')
public static void BuildProductChoices(Product product, PlaceHolder phChoices)
{
// ADD IN THE PRODUCT TEMPLATE CHOICES
ProductTemplate template = product.ProductTemplate;
if (template != null)
{
// DETERMINE VALIDATOR FIELDS - PRE-PROCESSING
bool RequireAllFields = true;
ArrayList requiredFields = new ArrayList();
Hashtable regexFields = new Hashtable();
if (product != null)
{
foreach (ProductTemplateField tf in product.TemplateFields)
{
if (tf.InputField.IsMerchantField)
{
// DETERMINE REQURIED FIELDS - empty value allowed
if (tf.InputField.Name.Replace(" ", "") == "RequiredFieldsList")
{
RequireAllFields = false;
if (!string.IsNullOrEmpty(tf.InputValue))
{
foreach (string fn in tf.InputValue.Split(",".ToCharArray()))
{
requiredFields.Add(fn);
}
}
}
// DETERMINE REGEX FIELDS - empty value ignored
if (!string.IsNullOrEmpty(tf.InputValue) &&
tf.InputField.Name.StartsWith("Regex") && tf.InputField.Name.EndsWith(" Field"))
{
//Find all matching regex merchant fields
string regexField = tf.InputField.Name.Split(' ')[0]; // Extract RegexN from "RegexN Field"
string regexExpression = string.Empty;
string regexMessage = string.Empty;
foreach (ProductTemplateField rtf in product.TemplateFields)
{
if (rtf.InputField.Name.StartsWith("Regex") && rtf.InputField.Name.EndsWith(" Expression"))
{
regexExpression = rtf.InputValue;
}
if (rtf.InputField.Name.StartsWith("Regex") && rtf.InputField.Name.EndsWith(" Message"))
{
regexMessage = rtf.InputValue;
}
if (!string.IsNullOrEmpty(regexExpression) && !string.IsNullOrEmpty(regexMessage))
{
RegexFieldValidator rgxfv = new RegexFieldValidator(regexField, regexExpression, regexMessage);
regexFields.Add(tf.InputValue, rgxfv);
break;
}
}
}
}
}
}
foreach (InputField input in template.InputFields)
{
if (!input.IsMerchantField)
{
// ADD THE CONTROL TO THE PLACEHOLDER
phChoices.Controls.Add(new LiteralControl("<tr><td colspan=\"2\">"));
phChoices.Controls.Add(new LiteralControl((input.UserPrompt + "<br />")));
WebControl o = input.GetControl();
if (o != null)
{
phChoices.Controls.Add(o);
if (input.InputType == InputType.TextBox || input.InputType == InputType.TextArea)
{
// REGEX FIELD VALIDATORS
if (regexFields.Contains(input.Name))
{
RegexFieldValidator rgx = (RegexFieldValidator)regexFields[input.Name];
RegularExpressionValidator rgxfv = new RegularExpressionValidator();
rgxfv.ID = "rgxfv" + input.InputFieldId.ToString();
rgxfv.ControlToValidate = o.ID;
rgxfv.Text = "*";
rgxfv.ErrorMessage = rgx.ErrorMessage;
rgxfv.ValidationGroup = "AddToBasket";
rgxfv.ValidationExpression = rgx.RegularExpression;
phChoices.Controls.Add(rgxfv);
}
// REQUIRED FIELD VALIDATORS
if (RequireAllFields || requiredFields.Contains(input.Name))
{
RequiredFieldValidator rfv = new RequiredFieldValidator();
rfv.ID = "rfv" + input.InputFieldId.ToString();
rfv.ControlToValidate = o.ID;
rfv.Text = "*";
rfv.ErrorMessage = "Please enter " + input.Name;
rfv.ValidationGroup = "AddToBasket";
phChoices.Controls.Add(rfv);
}
}
}
phChoices.Controls.Add(new LiteralControl("</td></tr>"));
}
}
}
}
// SG End Customization - ZLA 2009-05-20
Code: Select all
if (!string.IsNullOrEmpty(tf.InputValue) && tf.InputField.IsMerchantField)
Code: Select all
if (!string.IsNullOrEmpty(tf.InputValue) && tf.InputField.IsMerchantField &&
!tf.InputField.Name.StartsWith("Regex") &&
tf.InputField.Name.Replace(" ", "") != "RequiredFieldsList")
Code: Select all
// SG Customization - ZLA 2009-05-20 - prevent regular expression merchant fields from displaying
OrderItemInputCollection _OrderItemInputCollection = new OrderItemInputCollection();
//public OrderItemInputCollection OrderItemInputCollection
//{
// get { return _OrderItemInputCollection; }
// set { _OrderItemInputCollection = value; }
//}
// SG End Customization - ZLA 2009-05-20
Code: Select all
//SHOW INPUTS
if (_OrderItem.Inputs.Count > 0)
{
InputList.DataSource = _OrderItem.Inputs;
InputList.DataBind();
}
else
Code: Select all
//SHOW INPUTS
if (_OrderItem.Inputs.Count > 0)
{
// SG Customization - ZLA 2009-05-20 - prevent regular expression merchant fields from displaying
// SG Customization - ZLA 2009-05-20 - prevent required fields merchant field from displaying
foreach (OrderItemInput oii in _OrderItem.Inputs)
{
if (!oii.Name.StartsWith("Regex") && oii.Name.Replace(" ", "") != "RequiredFieldsList")
{
_OrderItemInputCollection.Add(oii);
}
}
InputList.DataSource = _OrderItemInputCollection;
InputList.DataBind();
// SG End Customization - ZLA 2009-05-20
}
else
Please let me know if you find this useful or have other comments. -- ZLA