私は最大値と最小値を含むオブジェクトを持っているので、すべての編集ビューと作成ビュー内で、ユーザーが最小値よりも大きい最小値を挿入した場合にクライアント側の検証メッセージを表示したいと考えています。ブラジル
1 に答える
1
おそらく、クライアント側とサーバー側の両方の検証が必要になるでしょう。独自の Validation を実装するとよいでしょう。あるフィールドが別のフィールドと同じ値ではないことを確認したいという同様のことをしました。私はゼロから始めたわけではありません。Microsoft の Simon J Ince のコードを使用しました。彼のブログにそれがあります。基本的に、彼は条件付き検証 (別のフィールドの値に基づいて検証する) の基盤を持っています。彼の ConditionalAttributeBase から継承し、IClientValidatable インターフェイスを実装しました (これは、Java スクリプトがチェックする依存フィールドをブラウザーに送信する方法です)。
/// <summary>
/// A validator for ensuring that the value of this field does NOT equal the value of another field.
/// </summary>
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
public class NotEqual:ConditionalAttributeBase, IClientValidatable
{
public string DependentProperty { get; set; }
/// <summary>
/// Returns client validation rules for NotEqual
/// </summary>
/// <param name="metadata">The model metadata.</param>
/// <param name="context">The controller context.</param>
/// <returns>
/// The client validation rules for NotEqual.
/// </returns>
IEnumerable<ModelClientValidationRule> IClientValidatable.GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationRule
{
ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()),
ValidationType = "notequal",
};
string depProp = BuildDependentPropertyId(metadata, context as ViewContext);
rule.ValidationParameters.Add("dependentproperty", depProp);
yield return rule;
}
/// <summary>
/// Builds the dependent property id.
/// </summary>
/// <param name="metadata">The metadata.</param>
/// <param name="viewContext">The view context.</param>
/// <returns></returns>
protected string BuildDependentPropertyId(ModelMetadata metadata, ViewContext viewContext)
{
return QualifyFieldId(metadata, DependentProperty, viewContext);
}
/// <summary>
/// Validates that the value does not equal the value of the dependent value if the dependent value is not null
/// </summary>
/// <param name="value">The value to validate.</param>
/// <param name="validationContext">The context information about the validation operation.</param>
/// <returns>
/// An instance of the <see cref="T:System.ComponentModel.DataAnnotations.ValidationResult"/> class.
/// </returns>
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
// check if the current value matches the target value
if (value != null && GetDependentFieldValue(DependentProperty, validationContext).ToString() == value.ToString())
{
return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
}
return ValidationResult.Success;
}
/// <summary>
/// Initializes a new instance of the <see cref="NotEqual"/> class.
/// </summary>
/// <param name="dependentProperty">The dependent property.</param>
public NotEqual(string dependentProperty)
{
DependentProperty = dependentProperty;
}
}$
次に、javascript を微調整しました。
(function ($) {
$.validator.addMethod('notequal',
function (value, element, parameters) {
var id = '#' + parameters['dependentproperty'];
var depControl = $(id);
var control = $(element);
if (control.val() === depControl.val())
return "";
return true;
}
);
$.validator.unobtrusive.adapters.add(
'notequal',
['dependentproperty'],
function (options) {
options.rules['notequal'] = {
dependentproperty: options.params['dependentproperty']
};
options.messages['notequal'] = options.message;
}
);
$.validator.addMethod('notequaltwo',
function (value, element, parameters) {
var id = '#' + parameters['dependentproperty'];
var depControl = $(id);
var control = $(element);
if (control.val() === depControl.val())
return "";
return true;
}
);
$.validator.unobtrusive.adapters.add(
'notequaltwo',
['dependentproperty'],
function (options) {
options.rules['notequaltwo'] = {
dependentproperty: options.params['dependentproperty']
};
options.messages['notequaltwo'] = options.message;
}
);
})(jQuery);$
私のコードを自分のやりたいように適応させる方法を理解していただければ幸いです。基本的には、型を変換する必要があります (型が変換されない場合は、何もしないでください。他のバリデーターを使用する必要があります)。 ) そして、比較を行います。
于 2012-05-08T14:18:45.413 に答える