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.
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.
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
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
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
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:
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
Hi Nik!
Oooh - that looks promising! Thanks a bunch - I'll give it a go :-)
/Chriztian
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:
So then I call the
@ValidStateClass()
function on the field wrapper:Thanks again Nik!
/Chriztian
No problem Chriztian, pleased I could help :-)
After some time, still this solution worked for me. Thank you so much Chriztian!
is working on a reply...