2011-11-30 33 views
6

問題:ASP.NET MVC4控えめな検証ローカライズ

私は問題は、デフォルトのメッセージは暗黙の[必須]にローカライズされるようになってきたが邪魔にならないjqueryの検証を使用して属性。私は、私のモデルと関連付けられたressourceファイル内のすべてのint(および他のnullableではない型)に[Required]を置いてはいけません。誰かがASP.NET MVC4 Dev Previewをテストして同じ問題に気づいたのですか?私はmvcコードを見ると、明らかに動作するはずです。

未遂ソリューション:Global.asaxの中

を追加しました:

DefaultModelBinder.ResourceClassKey = "ErrorMessages"; 

はグローバルリソースで "ErrorMessages.resx" と "ErrorMessages.fr.resx" と呼ばれるリソースファイルを持っていますPropertyValueInvalidおよびPropertyValueRequiredです。

興味深い情報:

私が気づいた良いことは、彼らは「フィールドは数字でなければなりません」またはハードディスク内部の密閉されたクラスで符号化されているから「フィールドが日付でなければならない」固定ということです。

ClientDataTypeModelValidatorProvider.ResourceClassKey = "ErrorMessages"; 

は、グローバルressourcesに「ErrorMessages.resx」と「ErrorMessages.fr.resx」というリソースファイルがあるフォルダ場合は動作およびFieldMustBeNumeric/FieldMustBeDateん

+2

これはMVC2/3で動作していて、v4のプレビューで壊れていますか? – RickAndMSFT

答えて

2

私は、これは古いですが、それだけには知っていますメタデータにローカライズされたメッセージを取得するには、DataAnnotationsModelValidatorをサブクラス化し、GetClientValidationRulesとValidateをオーバーライドして独自のメッセージを提供します。

DataAnnotationsModelValidatorProvider.RegisterAdapterFactoryを使用してアダプタを登録します。

ファクトリデリゲートを作成するためのラッパーファクトリビルダを作成しました。ここではoutパラメータは、リフレクションを介してアセンブリ内のすべてのアダプタを検出するときにループ内で使用するので、RegisterAdpaterFactoryを呼び出すために各アダプタの属性タイプを取得する必要があります。私はこれもMVC3で動作し、私もMVC2を考える情報

public static class ModelValidationFactory 
{ 
    /// <summary> 
    /// Builds a Lamda expression with the Func&lt;ModelMetadata, ControllerContext, ValidationAttribute, ModelValidator&gt; signature 
    /// to instantiate a strongly typed constructor. This used by the <see cref="DataAnnotationsModelValidatorProvider.RegisterAdapterFactory"/> 
    /// and used (ultimately) by <see cref="ModelValidatorProviderCollection.GetValidators"/> 
    /// </summary> 
    /// <param name="adapterType">Adapter type, expecting subclass of <see cref="ValidatorResourceAdapterBase{TAttribute}"/> where T is one of the <see cref="ValidationAttribute"/> attributes</param> 
    /// <param name="attrType">The <see cref="ValidationAttribute"/> generic argument for the adapter</param> 
    /// <returns>The constructor invoker for the adapter. <see cref="DataAnnotationsModelValidationFactory"/></returns> 
    public static DataAnnotationsModelValidationFactory BuildFactory(Type adapterType, out Type attrType) 
    { 
     attrType = adapterType.BaseType.GetGenericArguments()[0]; 

     ConstructorInfo ctor = adapterType.GetConstructor(new[] { typeof(ModelMetadata), typeof(ControllerContext), attrType }); 

     ParameterInfo[] paramsInfo = ctor.GetParameters(); 

     ParameterExpression modelMetadataParam = Expression.Parameter(typeof(ModelMetadata), "metadata"); 
     ParameterExpression contextParam = Expression.Parameter(typeof(ControllerContext), "context"); 
     ParameterExpression attributeParam = Expression.Parameter(typeof(ValidationAttribute), "attribute"); 

     Expression[] ctorCallArgs = new Expression[] 
     { 
      modelMetadataParam, 
      contextParam, 
      Expression.TypeAs(attributeParam, attrType) 
     }; 

     NewExpression ctorInvoker = Expression.New(ctor, ctorCallArgs); 

     // (ModelMetadata metadata, ControllerContext context, ValidationAttribute attribute) => new {AdapterType}(metadata, context, ({AttrType})attribute) 
     return Expression 
      .Lambda(typeof(DataAnnotationsModelValidationFactory), ctorInvoker, modelMetadataParam, contextParam, attributeParam) 
      .Compile() 
      as DataAnnotationsModelValidationFactory; 
    } 
} 

を登録をインライン化が、私は、アダプタを使用して、この後、他のものを行う/属性ことができました。

関連する問題