Browse Source
Merge pull request #4097 from abpframework/maliming/StringLengthAttribute-ErrorMessage
Change the ErrorMessage of StringLengthAttribute according to the situation.
pull/4127/head
Halil İbrahim Kalkan
6 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with
18 additions and
7 deletions
framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Validation/ValidationAttributeHelper.cs
framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Validation/ValidationTestController.cs
framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Validation/ValidationTestController_Tests.cs
@ -1,8 +1,5 @@
using System ;
using System.Collections.Generic ;
using System.ComponentModel.DataAnnotations ;
using System.ComponentModel.DataAnnotations ;
using System.Reflection ;
using System.Text ;
namespace Volo.Abp.AspNetCore.Mvc.Validation
{
@ -11,8 +8,22 @@ namespace Volo.Abp.AspNetCore.Mvc.Validation
private static readonly PropertyInfo ValidationAttributeErrorMessageStringProperty = typeof ( ValidationAttribute )
. GetProperty ( "ErrorMessageString" , BindingFlags . Instance | BindingFlags . NonPublic ) ;
private static readonly PropertyInfo ValidationAttributeCustomErrorMessageSetProperty = typeof ( ValidationAttribute )
. GetProperty ( "CustomErrorMessageSet" , BindingFlags . Instance | BindingFlags . NonPublic ) ;
public static void SetDefaultErrorMessage ( ValidationAttribute validationAttribute )
{
if ( validationAttribute is StringLengthAttribute stringLength & & stringLength . MinimumLength ! = 0 )
{
var customErrorMessageSet = ValidationAttributeCustomErrorMessageSetProperty . GetValue ( validationAttribute ) as bool? ;
if ( customErrorMessageSet ! = true )
{
validationAttribute . ErrorMessage =
"The field {0} must be a string with a minimum length of {2} and a maximum length of {1}." ;
return ;
}
}
validationAttribute . ErrorMessage =
ValidationAttributeErrorMessageStringProperty . GetValue ( validationAttribute ) as string ;
}
@ -35,7 +35,7 @@ namespace Volo.Abp.AspNetCore.Mvc.Validation
public class ValidationTest1Model
{
[Required]
[MinLength( 2)]
[StringLength(5, MinimumLength = 2)]
public string Value1 { get ; set ; }
}
@ -53,4 +53,4 @@ namespace Volo.Abp.AspNetCore.Mvc.Validation
}
}
}
}
@ -30,7 +30,7 @@ namespace Volo.Abp.AspNetCore.Mvc.Validation
{
var result = await GetResponseAsObjectAsync < RemoteServiceErrorResponse > ( "/api/validation-test/object-result-action?value1=a" , HttpStatusCode . BadRequest ) ; //value1 has min length of 2 chars.
result . Error . ValidationErrors . Length . ShouldBeGreaterThan ( 0 ) ;
result . Error . ValidationErrors [ 0 ] . Message . ShouldBe ( "Değer Bir alanı en az '2' uzunluğunda bir metin ya da dizi olmalıdır." ) ;
result . Error . ValidationErrors [ 0 ] . Message . ShouldBe ( "Değer Bir alanı en az 2, en fazla 5 uzunluğunda bir metin olmalıdır." ) ;
}
}