Copied to clipboard

Flag this post as spam?

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


  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Feb 06, 2018 @ 12:32
    Chriztian Steinmeier
    0

    UmbracoForms Themes set error class on wrapper

    Hi all,

    I've created a new folder in the Views/Partials/Forms/Themes folder so I can add an error class on the <div class="umbraco-forms-field"> wrapper for a field (if it has a validation error of course).

    I can see I need to change something in the Form.cshtml file - but how do I check if the field being rendered has any errors?

    Update: I can see the logic for displaying the validationmessage uses Model.ShowFieldValidaton (yep - stuck with a typo'ed propertyname) and I guess that could be used; just doesn't feel quite right...

    Thing is, I'm not using Visual Studio so I can't dot-code my way and discover if there's better way. So hoping someone knows or can point me to code/docs that actually explains how to accomplish this.

    /Chriztian

  • Nik 1625 posts 7295 karma points MVP 7x c-trib
    Feb 06, 2018 @ 16:51
    Nik
    0

    Hey Chriztian,

    I think that is the way you are meant to do it, it's the only way I've seen to do it :-)

    Nik

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Feb 09, 2018 @ 14:20
    Chriztian Steinmeier
    0

    Hi Nik,

    How have you seen this work? From what I can see, that part of the code renders en entire <span> that's either empty or containing the error meassage. All I need is a flag that tells me if the field is essentially "Valid" or "Invalid".

    It's absolutely necessary to be able to style anything that I can set a class on the field wrapper, indicating the state.

    /Chriztian

  • Nik 1625 posts 7295 karma points MVP 7x c-trib
    Feb 09, 2018 @ 14:33
    Nik
    0

    Hi Chriztian,

    I miss read what you were trying to do but you can relatively easily roll your own check for errors:

    All errors, as I understand it, are stored in the ViewData.ModelState

    So the checks are as follow:

    1. ViewData.ModelState.ContainsKey(##fieldname##)
    2. ViewData.ModelState[##fieldname##].Errors.Any()

    If both of these are true, then there are validation errors for a field.

    You could wrap this into a helper method so you can simply just call it each time.

    Nik

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Feb 09, 2018 @ 14:35
    Chriztian Steinmeier
    0

    Hi Nik!

    Oooh - that looks promising! Thanks a bunch - I'll give it a go :-)

    /Chriztian

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Feb 09, 2018 @ 16:43
    Chriztian Steinmeier
    1

    Here's what I did, which works for my purpose:

    In /Views/Partials/Forms/Themes/CustomTheme/Form.cshtml I added two functions at the bottom:

    @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;
        }
    }
    

    So then I call the @ValidStateClass() function on the field wrapper:

    <div class="@Html.GetFormFieldWrapperClass(f.FieldTypeName) @f.CssClass @ValidStateClass(f.Id)" @{ if (hidden) { <text> style="display: none" </text> } }>
    

    Thanks again Nik!

    /Chriztian

  • Nik 1625 posts 7295 karma points MVP 7x c-trib
    Feb 09, 2018 @ 16:45
    Nik
    0

    No problem Chriztian, pleased I could help :-)

  • Vasilis Fotopoulos 1 post 71 karma points
    Nov 10, 2022 @ 15:34
    Vasilis Fotopoulos
    0

    After some time, still this solution worked for me. Thank you so much Chriztian!

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies