protected override bool Validate()
{
string controlValue = ControlToValidate.Text.Trim();
bool valid = true;
if (IsEnabled && ControlToValidate.Enabled)
{
String minInclusive = null;
String minExclusive = null;
String maxInclusive = null;
String maxExclusive = null;
String length = null;
String minLength = null;
String maxLength = null;
var errorMessage = new StringBuilder();
XmlSchemaAttribute attribute = null;
SchemaManager.FindAttribute(_targetNamespace, _typeName, _attributeName, out attribute);
if (attribute == null)
{
LogManager.Error("XSDSchemaValidator - Failed to locate attribute: {0} in {1}", _attributeName, _targetNamespace);
}
else
{
XmlSchemaUse use = attribute.Use;
XmlSchemaSimpleType attributeSchemaType = attribute.AttributeSchemaType;
XmlQualifiedName qname = attributeSchemaType.BaseXmlSchemaType.QualifiedName;
String typeName = qname.Name;
ErrorProvider.SetError(_controlToValidate, "");
if (attributeSchemaType.Content is XmlSchemaSimpleTypeRestriction)
{
var restr = (XmlSchemaSimpleTypeRestriction) attributeSchemaType.Content;
foreach (object facet in restr.Facets)
{
if (facet is XmlSchemaMinInclusiveFacet)
{
var lf = facet as XmlSchemaMinInclusiveFacet;
if (!String.IsNullOrEmpty(controlValue))
{
int iResult;
double dResult;
if ((typeName.Contains("int") && int.TryParse(controlValue, out iResult) &&
iResult < int.Parse(lf.Value))
||
(typeName.Contains("dou") && double.TryParse(controlValue, out dResult) &&
dResult < double.Parse(lf.Value)))
{
errorMessage.Append((errorMessage.Length > 0) ? "\r\n" : "")
.Append(String.Format("The value must not be less than {0}", lf.Value));
}
}
}
else if (facet is XmlSchemaMinExclusiveFacet)
{
minExclusive = ((XmlSchemaMinExclusiveFacet) facet).Value;
}
else if (facet is XmlSchemaMaxInclusiveFacet)
{
maxInclusive = ((XmlSchemaMaxInclusiveFacet) facet).Value;
}
else if (facet is XmlSchemaMaxExclusiveFacet)
{
maxExclusive = ((XmlSchemaMaxExclusiveFacet) facet).Value;
}
else if (facet is XmlSchemaLengthFacet)
{
var lf = facet as XmlSchemaLengthFacet;
if (!String.IsNullOrEmpty(controlValue) && controlValue.Length > int.Parse(lf.Value))
{
errorMessage.Append((errorMessage.Length > 0) ? "\r\n" : "")
.Append(String.Format("The value's length must not excede {0} characters",
lf.Value));
}
}
else if (facet is XmlSchemaMinLengthFacet)
{
var lf = facet as XmlSchemaMinLengthFacet;
if (!String.IsNullOrEmpty(controlValue) && controlValue.Length < int.Parse(lf.Value))
{
errorMessage.Append((errorMessage.Length > 0) ? "\r\n" : "")
.Append(String.Format("The value's length must not be less than {0} characters",
lf.Value));
}
}
else if (facet is XmlSchemaMaxLengthFacet)
{
var lf = facet as XmlSchemaMaxLengthFacet;
if (!String.IsNullOrEmpty(controlValue) && controlValue.Length > int.Parse(lf.Value))
{
errorMessage.Append((errorMessage.Length > 0) ? "\r\n" : "")
.Append(
String.Format("The value's length must not be greater than {0} characters",
lf.Value));
}
}
else if (facet is XmlSchemaPatternFacet)
{
var spf = facet as XmlSchemaPatternFacet;
//Console.WriteLine(spf.Id + " : " + spf.Value);
if (!Regex.IsMatch(controlValue, spf.Value))
{
if (_icon != null)
ErrorProvider.Icon = _icon;
errorMessage.Append((errorMessage.Length > 0) ? "\r\n" : "")
.Append(String.Format(
"The \"{0}\" value must match the regular expression: {1}",
_attributeName, spf.Value));
valid = false;
}
}
else if (facet is XmlSchemaEnumerationFacet)
{
}
else if (facet is XmlSchemaTotalDigitsFacet)
{
}
else if (facet is XmlSchemaWhiteSpaceFacet)
{
}
}
ErrorProvider.SetError(_controlToValidate, errorMessage.ToString());
}
}
}
if (!valid)
{
if (ControlToValidate is ATMLControl)
((ATMLControl)ControlToValidate).HasErrors = true;
}
return valid;
}