Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Simon 692 posts 1068 karma points
    Nov 24, 2015 @ 08:54
    Simon
    0

    Umbraco Forms Custom Validation

    Hi Guys,

    I need some help regarding the form validate event of the Umbraco Forms.

    The event is doing the work that I am instructing, that validating email address and password matching of a registration form but errors are not being shown next to the fields.

    I have the below code:

     void UmbracoFormsController_FormValidate(object sender, Umbraco.Forms.Mvc.FormValidationEventArgs e)
            {
                if (e.Form.Name == "Registration Form")
                {
                    var s = sender as Controller;
                    if (s != null)
                    {
                        /*Validating Password*/
                        var pwf = e.Form.AllFields.SingleOrDefault(f => f.Alias == @ConstantNames.RegistrationPasswordPropertyAliasName).Id.ToString();
                        var rpwf = e.Form.AllFields.SingleOrDefault(f => f.Alias == @ConstantNames.RegistrationConfirmPasswordPropertyAliasName).Id.ToString();
    
                        var password = e.Context.Request[pwf];
                        var repeatpassword = e.Context.Request[rpwf];
    
                        if (password != repeatpassword)
                            s.ModelState.AddModelError(rpwf, "Password does not match!");
    
                        /*Validating Email Address, if it exists*/
                        var memberService = ApplicationContext.Current.Services.MemberService;
                        var emailAddress = e.Form.AllFields.SingleOrDefault(f => f.Alias == @ConstantNames.RegistrationEmailPropertyAliasName).Id.ToString();
    
                        var userEmailAddress = e.Context.Request[emailAddress];
    
                        if (memberService.GetByEmail(userEmailAddress) != null)
                        {
                            /*email exists*/
                            s.ModelState.AddModelError(emailAddress, "Email Already Exists!");
    
                        }
    
                    }
                }
            }
    

    Now, even when a debugged this problem, when I insert a breakpoint in the form.cshtml, after the valdiation it does not enter.

    Basically I want to inform the user about the errors because as it is, if user try to register with an incorrect email address that is already exists or with passwords that does not match, it just refreshes the page with empty fields, with any error info.

    Any ideas or help please?

    Thank you in advance.

  • Simon 692 posts 1068 karma points
    Dec 21, 2015 @ 20:41
    Simon
    0

    Anyone can help me please?

    Thank you.

  • Paul 89 posts 167 karma points
    Jan 25, 2017 @ 15:51
    Paul
    0

    Hi Simon,

    Did you ever figure this out?

    I need to support an old website which uses Umbraco Forms and get the error:

    UmbracoFormsController does not contain a definition for a 'FormValidate'
    

    Thanks,

    Paulius

  • M N 125 posts 212 karma points
    Nov 14, 2019 @ 00:26
    M N
    1

    Late to the game, sure you solved this by now but thought I would drop an example for anyone else.

    Working example below.. Although we do custom validation on specific Workflows. Aka If a form has a workflow (guid), execute this extra validation, rather than using the Form Name or Form Id. Found it a bit more re-usable for some of our more advanced workflows, like processing credit cards with Uforms and that sort of thing.

    But sounds like missing event binding!

    += UmbracoForms_FormValidate

        public class UmbracoFormsValidationOnWorkflow : ApplicationEventHandler
        {
    
            protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                // Umbraco started, hook into u forms events
                Umbraco.Forms.Web.Controllers.UmbracoFormsController.FormValidate += UmbracoForms_FormValidate; 
    
                Umbraco.Forms.Data.Storage.RecordStorage.RecordInserting += RecordStorage_RecordInserting;
    
                RecordStorage.RecordUpdating += RecordStorage_RecordUpdating;
            }
    
            private void UmbracoForms_FormValidate(object sender, Umbraco.Forms.Mvc.FormValidationEventArgs e)
            {
                Form theForm = e.Form;
    
                // get the workflows
                List<Workflow> workflows = wfs.GetAllWorkFlows(theForm);
    
                // get a specific workflow
                Workflow specificWorkflow = workflows.FirstOrDefault(a => a.WorkflowTypeId.ToString().ToLower() == "ffbbccdd-e7a6-44db-95aa-5acdccee470f"); 
    
    
                // custom validation if a specific workflow exists on the form 
                if (specificWorkflow != null)
                {
                    // grab a workflow field value
                    string workflowsettingvalue = specificWorkflow.Settings["SomeFieldValue"];
                    .. do more validation because this workflow is on the form
                }
            }
    
            private void RecordStorage_RecordUpdating(object sender, RecordEventArgs e)
            {
                Form form = e.Form;
            }
    
            private void RecordStorage_RecordInserting(object sender, RecordEventArgs e)
            {
                Form form = e.Form;
            }
    
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft