using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Text.RegularExpressions;
using System.Web;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
namespace Motion10.SharePoint2007 { /// <summary>
/// Field that validates it value by the given regular expression
/// </summary>
[Guid("92A8DAFE-92C5-407c-A1E6-7BF0C80FB904")] public class SPFieldRegexMatch : SPFieldText { /// <summary>
/// Initializes a new instance of the <see cref="SPFieldRegexMatch"/> class.
/// </summary>
/// <param name="fields">The fields.</param>
/// <param name="fieldName">Name of the field.</param>
public SPFieldRegexMatch(SPFieldCollection fields, string fieldName)
: base(fields, fieldName) { base.MaxLength = this.MaxLength;
}
/// <summary>
/// Initializes a new instance of the <see cref="SPFieldRegexMatch"/> class.
/// </summary>
/// <param name="fields">An <see cref="T:Microsoft.SharePoint.SPFieldCollection"></see> object that represents the field collection.</param>
/// <param name="typeName">A string that contains the name of the field type, which can be a string representation of an <see cref="T:Microsoft.SharePoint.SPFieldType"></see> value.</param>
/// <param name="displayName">A string that contains the display name of the field.</param>
public SPFieldRegexMatch(SPFieldCollection fields, string typeName, string displayName)
: base(fields, typeName, displayName) { base.MaxLength = this.MaxLength;
}
/// <summary>
/// Gets or sets the maximum number of characters that can be typed in the field.
/// </summary>
/// <value></value>
/// <returns>
/// A 32-bit integer that specifies the maximum number of characters.
/// </returns>
new public virtual int MaxLength { get { string propVal = this.GetCustomProperty("MaxLength") + "";
int retVal;
if(int.TryParse(propVal, out retVal)){ return retVal;
}
return 0xff;
}
set { this.SetCustomProperty("MaxLength", value); base.MaxLength = value;
}
}
/// <summary>
/// Gets or sets the validation expression.
/// </summary>
/// <value>The validation expression.</value>
public virtual string ValidationExpression { get { return this.GetCustomProperty("ValidationExpression") + ""; } set { this.SetCustomProperty("ValidationExpression", value); } }
/// <summary>
/// Gets or sets the error message.
/// </summary>
/// <value>The error message.</value>
public virtual string ErrorMessage { get { string retVal = this.GetCustomProperty("ErrorMessage") + ""; ; if (string.IsNullOrEmpty(retVal)) { retVal = string.Concat(this.Title,
" does not match the regular expression: ",
HttpUtility.HtmlEncode(this.ValidationExpression));
}
return retVal;
}
set { this.SetCustomProperty("ErrorMessage", value); } }
/// <summary>
/// Used for data serialization logic and for field validation logic that is specific to a custom field type to convert the field value object into a validated, serialized string.
/// </summary>
/// <param name="value">An object that represents the value object to convert.</param>
/// <returns>
/// A string that serializes the value object.
/// </returns>
[SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
public override string GetValidatedString(object value) { string retVal = base.GetValidatedString(value);
string textValue = base.GetFieldValueAsText(value);
if (!string.IsNullOrEmpty(textValue) && !string.IsNullOrEmpty(this.ValidationExpression)) { Regex validationRegex = null;
try { validationRegex = new Regex(this.ValidationExpression);
}
catch (ArgumentException) { throw new SPFieldValidationException("The configured regular expression is not valid. Please contact an administrator of this list to correct the issue."); }
if (!validationRegex.IsMatch(textValue)) { throw new SPFieldValidationException(this.ErrorMessage);
}
}
return retVal;
}
}
}