正如我在上面的评论中所说,我使用反射做了类似的事情。您可以忽略其中的一些,例如您可能不需要字典,因为这只是为他们提供自定义可翻译消息的一种方式。
服务器端代码:
private static Dictionary<string, ILocalisationToken> _requiredValidationDictionary;
private static Dictionary<string, ILocalisationToken> RequiredValidationDictionary(UserBase model)
{
if (_requiredValidationDictionary != null)
return _requiredValidationDictionary;
_requiredValidationDictionary = new Dictionary<string, ILocalisationToken>
{
{ model.GetPropertyName(m => m.Publication), ErrorMessageToken.PublicationRequired},
{ model.GetPropertyName(m => m.Company), ErrorMessageToken.CompanyRequired},
{ model.GetPropertyName(m => m.JobTitle), ErrorMessageToken.JobTitleRequired},
{ model.GetPropertyName(m => m.KnownAs), ErrorMessageToken.KnownAsRequired},
{ model.GetPropertyName(m => m.TelephoneNumber), ErrorMessageToken.TelephoneNoRequired},
{ model.GetPropertyName(m => m.Address), ErrorMessageToken.AddressRequired},
{ model.GetPropertyName(m => m.PostCode), ErrorMessageToken.PostCodeRequired},
{ model.GetPropertyName(m => m.Country), ErrorMessageToken.CountryRequired}
};
return _requiredValidationDictionary;
}
internal static void SetCustomRequiredFields(List<string> requiredFields, UserBase model, ITranslationEngine translationEngine)
{
if (requiredFields == null || requiredFields.Count <= 0) return;
var tokenDictionary = RequiredValidationDictionary(model);
//Loop through requiredFields and add Display text dependant on which field it is.
foreach (var requiredField in requiredFields.Select(x => x.Trim()))
{
ILocalisationToken token;
if (!tokenDictionary.TryGetValue(requiredField, out token))
token = LocalisationToken.GetFromString(string.Format("{0} required", requiredField));
//add to the model.
model.RequiredFields.Add(new RequiredField
{
FieldName = requiredField,
ValidationMessage = translationEngine.ByToken(token)
});
}
}
internal static void CheckForRequiredField<T>(ModelStateDictionary modelState, T fieldValue, string fieldName, IList<string> requiredFields, Dictionary<string, ILocalisationToken> tokenDictionary)
{
ILocalisationToken token;
if (!tokenDictionary.TryGetValue(fieldName, out token))
token = LocalisationToken.GetFromString(string.Format("{0} required", fieldName));
if (requiredFields.Contains(fieldName) && (Equals(fieldValue, default(T)) || string.IsNullOrEmpty(fieldValue.ToString())))
modelState.AddModelError(fieldName, token.Translate());
}
internal static void CheckForModelErrorForCustomRequiredFields(UserBase model, Paladin3DataAccessLayer client, ICache cache, ModelStateDictionary modelState)
{
var requiredFields = Common.CommaSeparatedStringToList (client.GetSettingValue(Constants.SettingNames.RequiredRegistrationFields, cache: cache, defaultValue: String.Empty, region: null)).Select(x => x.Trim()).ToList();
var tokenDictionary = RequiredValidationDictionary(model);
foreach (var property in typeof(UserBase) .GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))
{
CheckForRequiredField(modelState, property.GetValue(model, null), property.Name, requiredFields, tokenDictionary);
}
}
在模型上,我们有一个List<RequiredField>
基本上是一个包含两个字符串的类,一个用于字段名称,一个用于错误消息。
一旦你将模型传递到视图中,如果你想检查服务器端,你需要一些 jQuery 来将验证内容添加到页面中。
客户端代码:
$("#YOURFORM").validate();
for (var x = 0; x < requiredFields.length; x++) {
var $field = $('#' + requiredFields[x].FieldName.trim());
if ($field.length > 0) {
$field.rules("add", {
required: true,
messages: {
required: "" + requiredFields[x].ValidationMessage
//required: "Required Input"
}
});
$field.parent().addClass("formRequired"); //Add a class so that the user knows its a required field before they submit
}
}
抱歉,如果其中任何一个不是很清楚。随时提出任何问题,我会尽力解释。