Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Localize validation message implemented. #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@ public IActionResult Index(TestEmailNewsletterViewModel viewModel)
return View();
}

[HttpGet]
public IActionResult Localization()
{
return View();
}

[ValidateRecaptcha]
[HttpPost]
public IActionResult Localization(TestEmailNewsletterViewModel viewModel)
{
if (ModelState.IsValid)
{
return RedirectToAction(nameof(ThankYou), new { name = viewModel.Name });
}

return View();
}

[HttpGet]
public IActionResult DarkTheme()
{
Expand Down
3 changes: 2 additions & 1 deletion sample/TestWebApp/Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
<li><a asp-controller="TestEmailNewsletterSignup" asp-action="DarkTheme">Dark theme</a></li>
<li><a asp-controller="TestEmailNewsletterSignup" asp-action="Compact">Compact size</a></li>
<li><a asp-controller="TestEmailNewsletterSignup" asp-action="Audio">Audio type</a></li>
<li><a asp-controller="TestEmailNewsletterSignup" asp-action="JqueryValidationDisabled">Jqyery validation disabled</a></li>
<li><a asp-controller="TestEmailNewsletterSignup" asp-action="Localization">Message localization</a></li>
<li><a asp-controller="TestEmailNewsletterSignup" asp-action="JqueryValidationDisabled">Jquery validation disabled</a></li>
</ul>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
@{
ViewBag.Title = "Signup for our awesome newsletter";
}
@model TestEmailNewsletterViewModel
@using PaulMiami.AspNetCore.Mvc.Recaptcha


<form asp-controller="TestEmailNewsletterSignup" asp-action="Localization" method="post" class="form-horizontal">
<h4>@ViewBag.Title</h4>
<hr />
<div asp-validation-summary="All" class="text-danger"></div>
<div class="form-group">
<label asp-for="Name" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="Email" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="Email" class="form-control" />
<span asp-validation-for="Email" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<recaptcha size="RecaptchaSize.Compact" />
<span class="text-danger" id="recaptchaErrorMessage"></span>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<button type="submit" class="btn btn-default">Signup</button>
</div>
</div>
</form>

@section Scripts {
<recaptcha-script validation-message-element-id="recaptchaErrorMessage" validation-message="Put here your localized &apos;Are you a robot?&apos;" />
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class RecaptchaScriptTagHelper : TagHelper

private const string JqueryValidationAttributeName = "jquery-validation";
private const string ValidationMessageElementIdAttributeName = "validation-message-element-id";
private const string ValidationMessageAttributeName = "validation-message";

public RecaptchaScriptTagHelper(IRecaptchaConfigurationService service, IHttpContextAccessor contextAccessor)
{
Expand All @@ -36,6 +37,9 @@ public RecaptchaScriptTagHelper(IRecaptchaConfigurationService service, IHttpCon
[HtmlAttributeName(ValidationMessageElementIdAttributeName)]
public string ValidationMessageElementId { get; set; }

[HtmlAttributeName(ValidationMessageAttributeName)]
public string ValidationMessage { get; set; }

public override void Process(TagHelperContext context, TagHelperOutput output)
{
if (!_service.Enabled)
Expand Down Expand Up @@ -63,7 +67,7 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
var script = new TagBuilder("script");
script.TagRenderMode = TagRenderMode.Normal;
script.InnerHtml.AppendHtml(string.Format(_scriptSnippet,
RecaptchaTagHelper.RecaptchaValidationJSCallBack, ValidationMessageElementId, _service.ValidationMessage));
RecaptchaTagHelper.RecaptchaValidationJSCallBack, ValidationMessageElementId, ValidationMessage ?? _service.ValidationMessage));

output.PostElement.AppendHtml(script);
}
Expand Down