本文整理匯總了C#中System.Web.Mvc.ModelClientValidationRule類的典型用法代碼示例。如果您正苦於以下問題:C# ModelClientValidationRule類的具體用法?C# ModelClientValidationRule怎麽用?C# ModelClientValidationRule使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
ModelClientValidationRule類屬於System.Web.Mvc命名空間,在下文中一共展示了ModelClientValidationRule類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: GetClientValidationRules
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationRule()
{
ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()),
ValidationType = "validname",
};
var clientValidationRules = new List<string>
{
_onlyOneEntityValidator.GetClientValidationRules(metadata,context).First().ValidationType,
_cannotBeNotApplicable.GetClientValidationRules(metadata,context).First().ValidationType,
_cannotContainNumbers.GetClientValidationRules(metadata,context).First().ValidationType,
};
var regexPatterns = new List<string>
{
_onlyOneEntityValidator.Pattern,
_cannotBeNotApplicable.Pattern,
_cannotContainNumbers.Pattern,
};
var errorMessages = new List<string>
{
_onlyOneEntityValidator.FormatErrorMessage(metadata.GetDisplayName()),
_cannotBeNotApplicable.FormatErrorMessage(metadata.GetDisplayName()),
_cannotContainNumbers.FormatErrorMessage(metadata.GetDisplayName())
};
rule.ValidationParameters.Add("clientvalidationrules", clientValidationRules.Aggregate((i, j) => i + "," + j));
rule.ValidationParameters.Add("regexpatterns", regexPatterns.Aggregate((i, j) => i + "," + j));
rule.ValidationParameters.Add("errormessages", errorMessages.Aggregate((i, j) => i + "," + j));
yield return rule;
}
示例2: GetClientValidationRules
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
ModelClientValidationRule ccvAttribute = new ModelClientValidationRule();
ccvAttribute.ErrorMessage = this.ErrorMessage;
ccvAttribute.ValidationType = "ccv";
yield return ccvAttribute;
}
示例3: GetClientValidationRules
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationRule()
{
ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()),
ValidationType = "requiredifany",
};
var properties = new List<string>();
var values = new List<object>();
for (int i = 0; i < DependentProperties.Count(); i++)
{
string depProp = BuildDependentPropertyId(metadata, context as ViewContext, DependentProperties[i]);
// find the value on the control we depend on;
// if it's a bool, format it javascript style
// (the default is True or False!)
string targetValue = (TargetValues[i] ?? "").ToString();
if (TargetValues[i].GetType() == typeof(bool))
targetValue = targetValue.ToLower();
properties.Add(depProp);
values.Add(targetValue);
}
rule.ValidationParameters.Add("dependentproperties", String.Join("|", properties));
rule.ValidationParameters.Add("targetvalues", String.Join("|", values));
yield return rule;
}
示例4: MessageOrDefault
protected string MessageOrDefault(ModelClientValidationRule rule, string propertyName)
{
// We don't want to display the default {validator.*} messages
if ((rule.ErrorMessage != null) && !rule.ErrorMessage.StartsWith("{validator."))
return rule.ErrorMessage;
switch (rule.ValidationType)
{
case "stringLength":
var maxLength = (int)rule.ValidationParameters["maximumLength"];
return
new StringLengthAttribute(maxLength).FormatErrorMessage(propertyName);
case "required":
return new RequiredAttribute().FormatErrorMessage(propertyName);
case "range":
var min = Convert.ToDouble(rule.ValidationParameters["minimum"]);
var max = Convert.ToDouble(rule.ValidationParameters["maximum"]);
return
new System.ComponentModel.DataAnnotations.RangeAttribute(min, max).FormatErrorMessage(propertyName);
case "regularExpression":
var pattern = (string)rule.ValidationParameters["pattern"];
return new RegularExpressionAttribute(pattern).FormatErrorMessage(propertyName);
default:
throw new NotSupportedException(
"Only stringLength, Required, Range and RegularExpression validators are supported for generic error messages. Add a custom error message or choose another validator type");
}
}
示例5: GetClientValidationRules
/// <summary>
/// When implemented in a class, returns client validation rules for that class.
/// </summary>
/// <param name="metadata">The model metadata.</param>
/// <param name="context">The controller context.</param>
/// <returns>
/// The client validation rules for this validator.
/// </returns>
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationRule { ErrorMessage = ErrorMessageString ?? "Field invalid. Field can only contain alphanumeric characters: 'a-z', 'A-Z', '0-9'.", ValidationType = clientValidationRule };
rule.ValidationParameters.Add("pattern", RootModuleConstants.AlphanumericExpression);
yield return rule;
}
示例6: GetClientValidationRules
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationRule();
rule.ValidationType = "gangmember";
rule.ErrorMessage = "You must be in the gang";
yield return rule;
}
示例7: GetClientValidationRules
/// <summary>
/// When implemented in a class, returns client validation rules for that class.
/// </summary>
/// <param name="metadata">The model metadata.</param>
/// <param name="context">The controller context.</param>
/// <returns>
/// The client validation rules for this validator.
/// </returns>
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationRule { ErrorMessage = ErrorMessageString ?? "Field must not contain HTML.", ValidationType = clientValidationRule };
rule.ValidationParameters.Add("pattern", RootModuleConstants.HtmlRegularExpression);
yield return rule;
}
示例8: GetClientValidationRules
/// <summary>
/// When implemented in a class, returns client validation rules for that class.
/// </summary>
/// <param name="metadata">The model metadata.</param>
/// <param name="context">The controller context.</param>
/// <returns>
/// The client validation rules for this validator.
/// </returns>
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationRule { ErrorMessage = ErrorMessageString ?? "The field must not contain any of the following characters: \" / \\ [ ] : ; | = , + * ? < > %", ValidationType = clientValidationRule };
rule.ValidationParameters.Add("pattern", RootModuleConstants.ActiveDirectoryNonCompliantExpression);
yield return rule;
}
示例9: GetClientValidationRules
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationRule();
rule.ErrorMessage = "true";
rule.ValidationType = "dbisunique";
yield return rule;
}
示例10: GetClientValidationRules
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationRule()
{
ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()),
ValidationType = (_invertToRequiredIfNot ? "requiredifnot" : "requiredif"),
};
string depProp = BuildDependentPropertyId(metadata, context as ViewContext);
// find the value on the control we depend on;
// if it's a bool, format it JavaScript style
// (the default is True or False!)
StringBuilder sb = new StringBuilder();
foreach (var obj in this._targetValues)
{
string targetValue = (obj ?? string.Empty).ToString();
if (obj.GetType() == typeof(bool))
targetValue = targetValue.ToLower();
sb.AppendFormat("|{0}", targetValue);
}
rule.ValidationParameters.Add("dependentproperty", depProp);
rule.ValidationParameters.Add("targetvalue", sb.ToString().TrimStart('|'));
yield return rule;
}
示例11: SetModelValidationRule
protected override void SetModelValidationRule(ModelClientValidationRule rule)
{
base.SetModelValidationRule(rule);
switch (rule.ValidationType)
{
case "required":
this.AllowBlank = false;
this.BlankText = rule.ErrorMessage;
break;
case "range":
if (rule.ValidationParameters.ContainsKey("max"))
{
this.MaxSelections = (int)Convert.ChangeType(rule.ValidationParameters["max"], typeof(int));
this.MaxSelectionsText = rule.ErrorMessage;
}
if (rule.ValidationParameters.ContainsKey("min"))
{
this.MinSelections = (int)Convert.ChangeType(rule.ValidationParameters["min"], typeof(int));
this.MinSelectionsText = rule.ErrorMessage;
}
break;
}
}
示例12: GetClientValidationRules
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationRule
{
ErrorMessage = string.Format(ErrorMessage, metadata.GetDisplayName()),
ValidationType = "requiredif",
};
var viewContext = (ViewContext)context;
var oldPrefix = viewContext.ViewData.TemplateInfo.HtmlFieldPrefix;
// only strip prefix when it exists
viewContext.ViewData.TemplateInfo.HtmlFieldPrefix = string.Empty;
var otherInputName = viewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(OtherProperty);
var thisPropertyName = ".{0}".FormatWith(metadata.PropertyName);
if (oldPrefix.EndsWith(thisPropertyName))
{
viewContext.ViewData.TemplateInfo.HtmlFieldPrefix = oldPrefix.Replace(thisPropertyName, string.Empty);
otherInputName = viewContext
.ViewData
.TemplateInfo
.GetFullHtmlFieldName(OtherProperty);
}
viewContext.ViewData.TemplateInfo.HtmlFieldPrefix = oldPrefix;
rule.ValidationParameters.Add("otherinputname", otherInputName);
rule.ValidationParameters.Add("comparisontype", ComparisonType);
rule.ValidationParameters.Add("othercomparisonvalue", OtherComparisonValue);
yield return rule;
}
示例13: GetClientValidationRules
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
string errorMessage = FormatErrorMessage("");
ModelClientValidationRule rule = new ModelClientValidationRule { ValidationType = "agerange", ErrorMessage = errorMessage };
rule.ValidationParameters.Add("minage", this.Minimum);
rule.ValidationParameters.Add("maxage", this.Maximum);
yield return rule;
}
示例14: GetClientValidationRules
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationRule();
rule.ErrorMessage = FormatErrorMessage(metadata.DisplayName);
rule.ValidationType = "startsafter";
rule.ValidationParameters.Add("date", DateTime.Today.ToString(CultureInfo.InvariantCulture));
yield return rule;
}
示例15: GetClientValidationRules
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var existsRule = new ModelClientValidationRule { ValidationType = "existsin", ErrorMessage = FormatErrorMessage(metadata.DisplayName ?? metadata.PropertyName) };
existsRule.ValidationParameters["collection"] = Collection;
existsRule.ValidationParameters["value"] = Value;
existsRule.ValidationParameters["display"] = Display;
yield return existsRule;
}