Copied to clipboard

Flag this post as spam?

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


  • Nathan Rogan 22 posts 203 karma points
    Sep 06, 2021 @ 11:45
    Nathan Rogan
    0

    Forms validation check / helper

    I need to add a custom class to a forms field if validation fails.

    This post is exactly what I need but it does not work - https://our.umbraco.com/forum/umbraco-forms/90433-umbracoforms-themes-set-error-class-on-wrapper

    CMS: 8.14.4 Forms: 8.7.6

    @functions {
    public bool HasErrors(string fieldId) {
        if (ViewData.ModelState.ContainsKey(fieldId)) {
            return ViewData.ModelState[fieldId].Errors.Any();
        } else {
            return false;
        }
    }
    
    public string ValidStateClass(string fieldId) {
        var klass = "";
        if (HasErrors(fieldId)) {
            klass = "invalid";
        }
        return klass;
    }
    

    }

    @ValidStateClass(f.Id)
    

    Does anyone know how to fix or have an alternative solution?

    Thanks

Please Sign in or register to post replies

Write your reply to:

Draft