Created
September 19, 2016 12:34
-
-
Save jonathanread/2b399b3cba6e7c147c94cf2cd95454c9 to your computer and use it in GitHub Desktop.
Sitefinity MVC ReCaptacha
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
using SitefinityWebApp.Mvc.Models; | |
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Linq; | |
using System.Web; | |
using Telerik.Sitefinity.Data.Metadata; | |
using Telerik.Sitefinity.Frontend.Forms; | |
using Telerik.Sitefinity.Frontend.Forms.Mvc.Controllers.Base; | |
using Telerik.Sitefinity.Model; | |
using Telerik.Sitefinity.Mvc; | |
using Telerik.Sitefinity.Web.UI.Fields.Enums; | |
namespace SitefinityWebApp.Mvc.Controllers { | |
[ControllerToolboxItem(Name = "Recaptcha", Title = "Recaptcha", Toolbox = FormsConstants.FormControlsToolboxName, SectionName = "Common")] | |
[DatabaseMapping(UserFriendlyDataType.ShortText)] | |
public class RecaptchaController : FormFieldControllerBase<RecaptchaModel> { | |
public string SiteKey { get; set; } | |
public RecaptchaController() { | |
this.DisplayMode = FieldDisplayMode.Write; | |
} | |
public override RecaptchaModel Model { | |
get { | |
if (this.model == null) | |
this.model = new RecaptchaModel(); | |
this.model.SiteKey = this.SiteKey; | |
return this.model; | |
} | |
} | |
private RecaptchaModel model; | |
} | |
} |
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using Telerik.Sitefinity.Frontend.Forms.Mvc.Models.Fields; | |
namespace SitefinityWebApp.Mvc.Models { | |
public class RecaptchaModel : FormFieldModel { | |
public string SubmitValue { | |
get; set; | |
} | |
public string SiteKey { get; set; } | |
public override object GetViewModel(object value) { | |
return base.GetViewModel(value); | |
} | |
public override object GetViewModel(object value, Telerik.Sitefinity.Metadata.Model.IMetaField metaField) { | |
this.Value = value as string ?? (this.MetaField != null ? this.MetaField.DefaultValue : null) ?? string.Empty; | |
return this; | |
} | |
} | |
} |
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
@model SitefinityWebApp.Mvc.Models.RecaptchaModel | |
@using Telerik.Sitefinity.Frontend.Mvc.Helpers; | |
@using Telerik.Sitefinity.Modules.Pages; | |
@using Telerik.Sitefinity.Services; | |
@Html.Script(ScriptRef.JQuery, "top", false) | |
@Html.Script("https://www.google.com/recaptcha/api.js", "head", false) | |
<div class="@Model.CssClass form-group"> | |
@if (Model.SiteKey.IsNullOrWhitespace()) | |
{ | |
@Html.Raw("PLease configure your sitekey") | |
} | |
else | |
{ | |
<div class="g-recaptcha recaptcha" data-sitekey="@Model.SiteKey" data-callback="onReturnCallback" data-theme="light"></div> | |
} | |
</div> | |
<script type="text/javascript"> | |
$(document).ready(function () { | |
var captchaForm = $('.g-recaptcha').parents('form').first(); | |
captchaForm.submit(function (e) { | |
debugger; | |
var response = grecaptcha.getResponse(); | |
if (response.length == 0) { | |
//reCaptcha not verified | |
e.preventDefault(); | |
grecaptcha.reset(); | |
} | |
}); | |
}); | |
function onReturnCallback() { | |
$(".sf-fieldWrp button").removeAttr("disabled"); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment