Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3134 from aspnetboilerplate/pr/2899
resolves #2899 Added localize extension method for CustomValidationContext
- Loading branch information
Showing
5 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
src/Abp/Runtime/Validation/CustomValidationContextExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using Abp.Localization; | ||
|
||
namespace Abp.Runtime.Validation | ||
{ | ||
public static class CustomValidationContextExtensions | ||
{ | ||
/// <param name="validationContext">Validation context</param> | ||
/// <param name="sourceName">Localization source name</param> | ||
/// <param name="key">Localization key</param> | ||
public static string Localize(this CustomValidationContext validationContext, string sourceName, string key) | ||
{ | ||
var localizationManager = validationContext.IocResolver.Resolve<ILocalizationManager>(); | ||
var source = localizationManager.GetSource(sourceName); | ||
return source.GetString(key); | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
test/Abp.Zero.SampleApp.Tests/Application/Services/Validation_Tests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Abp.Auditing; | ||
using Abp.Localization; | ||
using Abp.Runtime.Validation; | ||
using Abp.Zero.SampleApp.Users; | ||
using Abp.Zero.SampleApp.Users.Dto; | ||
using Shouldly; | ||
using Xunit; | ||
|
||
namespace Abp.Zero.SampleApp.Tests.Application.Services | ||
{ | ||
public class Validation_Tests : SampleAppTestBase | ||
{ | ||
private readonly IUserAppService _userAppService; | ||
|
||
public Validation_Tests() | ||
{ | ||
_userAppService = Resolve<IUserAppService>(); | ||
} | ||
|
||
|
||
[Theory] | ||
[InlineData("en")] | ||
[InlineData("en-US")] | ||
[InlineData("en-GB")] | ||
public void CustomValidationContext_Localize_Test(string cultureName) | ||
{ | ||
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(cultureName); | ||
|
||
var exception = Assert.Throws<AbpValidationException>(() => | ||
{ | ||
_userAppService.CustomValidateMethod(new CustomValidateMethodInput()); | ||
}); | ||
|
||
exception.ValidationErrors.ShouldNotBeNull(); | ||
exception.ValidationErrors.Count.ShouldBe(1); | ||
exception.ValidationErrors[0].ErrorMessage.ShouldBe("User is not in role."); | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
test/Abp.Zero.SampleApp/Users/Dto/CustomValidateMethodInput.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using Abp.Runtime.Validation; | ||
|
||
namespace Abp.Zero.SampleApp.Users.Dto | ||
{ | ||
public class CustomValidateMethodInput : ICustomValidate | ||
{ | ||
public void AddValidationErrors(CustomValidationContext context) | ||
{ | ||
var message = context.Localize(AbpZeroConsts.LocalizationSourceName, "Identity.UserNotInRole"); | ||
context.Results.Add(new ValidationResult(message)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters