Forked from TimGeyssens/Umbraco Forms validation example
Last active
November 26, 2015 22:13
-
-
Save csharpforevermore/101303f3159f85577ea5 to your computer and use it in GitHub Desktop.
Umbraco Forms validation example
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
public class FormsValidation : ApplicationEventHandler | |
{ | |
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) | |
{ | |
Umbraco.Forms.Web.Controllers.UmbracoFormsController.FormValidate += UmbracoFormsController_FormValidate; | |
} | |
void UmbracoFormsController_FormValidate(object sender, Umbraco.Forms.Mvc.FormValidationEventArgs e) | |
{ | |
if (e.Form.Name == "Workshop Signup") | |
{ | |
var s = sender as Controller; | |
if (s != null) | |
{ | |
var emf = e.Form.AllFields.SingleOrDefault(f => f.Caption == "Email").Id.ToString(); | |
var pwf = e.Form.AllFields.SingleOrDefault(f => f.Caption == "Password").Id.ToString(); | |
var email = e.Context.Request[emf]; | |
var password = e.Context.Request[pwf]; | |
if (Profiles.Login(email, password)) | |
{ | |
//check if there is a spot left, just count subnodes | |
var help = new UmbracoHelper(UmbracoContext.Current); | |
var workshop = help.TypedContent(UmbracoContext.Current.PageId); | |
var spotLeft = workshop.Children.Count() < Convert.ToInt32(workshop.GetPropertyValue("maxAttendees")); | |
if (!spotLeft) | |
{ | |
s.ModelState.AddModelError(pwf, "Sorry today's seats are gone, we'll release some more soon."); | |
} | |
} | |
else | |
{ | |
s.ModelState.AddModelError(pwf, "UmbracoID could not find your credentials, make sure you have an account on umbraco.com with a confirmed codegarden 2015 ticket."); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment