Server side validation after changing properties in macro
I have a form that I have created using standard HTML input tags in a razor macro that is shown on the frontend. The fields in the form have a one-to-one correlation with custom properties that I have defined in my member types in the backend. When the form is loaded, the fields get populated with the values from those non-empty properties in the backend. When a user presses the submit button the values in the form fields get passed back up to the custom properties in the members section.
I know this sounds a bit of a mess, but it is working for me. The problem I have concerns validation. I can (and have) easily implimented the jQuery Validation plugin for client side validation, but how can I perform server side validation on the properties?
I have tested things as such: I define a property in the back end as being required and save it with some value. Then through the form, post with that field empty. The code within the if(isPost) block happily runs and erases the values that I had entered in the backend. If I then try to hit the save button on the backend, the validation runs and tells me that the field is required. What I want is for that validation to run when I submit the form from the frontend.
Hi Josh, just to check i am understanding you correctly, the backoffice validation is working correctly and your front end form is not doing any validation...
In your isPost code you will need to manually add in all the validation you want before you create the node. If you are intending to have a ferw forms then i woudl suggest you look at the contour package http://our.umbraco.org/projects/umbraco-pro/contour ;
After re-reading my question I realize that I did not state it very clearly so let me take another crack at it:
Everything works properly in the backoffice. If I try to save a member and one of the properties does not meet the validation requirements, the backoffice properly informs me that there is a problem, as is the expected behavior. Good.
On the frontend form, which I have created by writing a razor macro with HTML controls and manual calls to the custom member property fileds I defined in the backoffice,the jQuery validation also works just fine, but, we all know that relying on client-side javascript based validation is a big no-no, you have to make sure to do server-side validation.
The problem is that because I have made a mess of things and I am going about things in a very backwards direction (I am sure). When I do my isPost on my form, all it does is manually change the fields but does not invoke any kind of server-side validation. I can, for example, empty a field on my form that is defined as a required field in the backoffice, hit the submit button on the form, watch in the backoffice as the field value is erased, and no complaints are raised from the backoffice. It happily goes along as if the validation never occured.
A brief example:
First I declare a local variable and set it to whatever is stored in the corresponding field in the backoffice:
var first = member.getProperty("firstName").Value.ToString();
The I check for isPost and if so grab the new value from the form and set the backoffice property as such:
Now, I am sure that I have done something wrong here, and I guess what I need to try and figure out is how to make sure that the backoffice does its validation run whenever I hit the submit button on my form, AND, if there is a problem how to inform the frontend form so that the appropriate message can be displayed.
I hope that clears things up just a bit.
In regards to Contour, I have taken it out for its paces and it is a great piece of software (and reasonably priced for the headaches it saves!) but unfortunatly it is just not flexible enough for our needs. The new version does allow for much greater flexibility, but after spending a full work week (40 hours) trying to beat it into submission and make it do what I wanted, I had to throw up my hands in frusturation. There are some very strange and very strict requirements that my companies owner has etched into stone for just about any project he assigns me, and this one is no different, so as usual I end up having to do almost everything by hand and reinvent the wheel over, and over, and over... Such is life.
The particular form we are looking at right now is a very long application form. The requirements are that a person registers on our website, that registration information is automatically put into the form, and the registered user is given access to their form. The user must be able to close the form and pick up later where they left off but with no expectation that it will be from the same computer (no cookies, but I can see ways around that in Contour). The form itself is also quite complex and must allow for multiple sets of multiple fields to be created dynamically (this one really stumped me in Contour). Beyond that, the styling requirements for the form are also somewhat difficult to work into Contour... possible, but difficult.
Anyway, hopefully you or someone else can point me in the right direction here. I really don't mind doing things by hand, oddly, time is the one thing that is not constrained here! Thanks in advance!
Hi Josh, i understand what you are trying to do, from what i know of umbraco there isnt a way to actually use the backoffice validation in custom code ... be it a form or anything else which updates/creates nodes.
So what i think you have to do is manually add in all the validation so where you have
I would set up a list ro something to capture errors and then go through each field and check its validity something like
List<string> errors = newList<string>();
string email = Request["email"];
if (string.IsNullOrEmpty(email))
{
errors.Add("<li>Email cannot be empty</li>");
}
else
{
var regex = new System.Text.RegularExpressions.Regex(@"^[A-Za-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[A-Za-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?\.)+(?:[A-Za-z]{2}|aero|arpa|biz|com|coop|cat|edu|gov|info|int|jobs|mil|mobi|museum|name|net|org|pro|travel)$");
if (!regex.IsMatch(email))
{
errors.Add("<li>Email is invalid</li>");
}
}
You would need to do this for each and every field, and i would actually make a custom class representing the form, populate that class with form fields, and then validate that object in a seperate helper method that way you can reuse the validation regex's etc. Either way if you do it inline or in a method you would then need to process the list of errors something like
if (errors.Count == 0)
{
//process the form
}
else
{
//show error message
}
Hey Asif, yeah, that makes sense. I was hoping there would be a way to, for instance, fake a 'save' button press in the backoffice from code thereby firing the validation and then using the result of that validation to decide what to do next (process form or show error message), but it was kinda a long shot. Like I said, I don't mind doing things by hand and taking the long way around, but I try not to reinvent the wheel except when i have to!
Server side validation after changing properties in macro
I have a form that I have created using standard HTML input tags in a razor macro that is shown on the frontend. The fields in the form have a one-to-one correlation with custom properties that I have defined in my member types in the backend. When the form is loaded, the fields get populated with the values from those non-empty properties in the backend. When a user presses the submit button the values in the form fields get passed back up to the custom properties in the members section.
I know this sounds a bit of a mess, but it is working for me. The problem I have concerns validation. I can (and have) easily implimented the jQuery Validation plugin for client side validation, but how can I perform server side validation on the properties?
I have tested things as such: I define a property in the back end as being required and save it with some value. Then through the form, post with that field empty. The code within the if(isPost) block happily runs and erases the values that I had entered in the backend. If I then try to hit the save button on the backend, the validation runs and tells me that the field is required. What I want is for that validation to run when I submit the form from the frontend.
Please help!
(I am using 4.11.1 and SQL Server)
Hi Josh, just to check i am understanding you correctly, the backoffice validation is working correctly and your front end form is not doing any validation...
In your isPost code you will need to manually add in all the validation you want before you create the node. If you are intending to have a ferw forms then i woudl suggest you look at the contour package http://our.umbraco.org/projects/umbraco-pro/contour ;
Hi Asif,
After re-reading my question I realize that I did not state it very clearly so let me take another crack at it:
Everything works properly in the backoffice. If I try to save a member and one of the properties does not meet the validation requirements, the backoffice properly informs me that there is a problem, as is the expected behavior. Good.
On the frontend form, which I have created by writing a razor macro with HTML controls and manual calls to the custom member property fileds I defined in the backoffice,the jQuery validation also works just fine, but, we all know that relying on client-side javascript based validation is a big no-no, you have to make sure to do server-side validation.
The problem is that because I have made a mess of things and I am going about things in a very backwards direction (I am sure). When I do my isPost on my form, all it does is manually change the fields but does not invoke any kind of server-side validation. I can, for example, empty a field on my form that is defined as a required field in the backoffice, hit the submit button on the form, watch in the backoffice as the field value is erased, and no complaints are raised from the backoffice. It happily goes along as if the validation never occured.
A brief example:
First I declare a local variable and set it to whatever is stored in the corresponding field in the backoffice:
The I check for isPost and if so grab the new value from the form and set the backoffice property as such:
I then re-set the local variable with the new value that was just stored:
and then the actual HTML control tag is as such:
Now, I am sure that I have done something wrong here, and I guess what I need to try and figure out is how to make sure that the backoffice does its validation run whenever I hit the submit button on my form, AND, if there is a problem how to inform the frontend form so that the appropriate message can be displayed.
I hope that clears things up just a bit.
In regards to Contour, I have taken it out for its paces and it is a great piece of software (and reasonably priced for the headaches it saves!) but unfortunatly it is just not flexible enough for our needs. The new version does allow for much greater flexibility, but after spending a full work week (40 hours) trying to beat it into submission and make it do what I wanted, I had to throw up my hands in frusturation. There are some very strange and very strict requirements that my companies owner has etched into stone for just about any project he assigns me, and this one is no different, so as usual I end up having to do almost everything by hand and reinvent the wheel over, and over, and over... Such is life.
The particular form we are looking at right now is a very long application form. The requirements are that a person registers on our website, that registration information is automatically put into the form, and the registered user is given access to their form. The user must be able to close the form and pick up later where they left off but with no expectation that it will be from the same computer (no cookies, but I can see ways around that in Contour). The form itself is also quite complex and must allow for multiple sets of multiple fields to be created dynamically (this one really stumped me in Contour). Beyond that, the styling requirements for the form are also somewhat difficult to work into Contour... possible, but difficult.
Anyway, hopefully you or someone else can point me in the right direction here. I really don't mind doing things by hand, oddly, time is the one thing that is not constrained here! Thanks in advance!
Hi Josh, i understand what you are trying to do, from what i know of umbraco there isnt a way to actually use the backoffice validation in custom code ... be it a form or anything else which updates/creates nodes.
So what i think you have to do is manually add in all the validation so where you have
I would set up a list ro something to capture errors and then go through each field and check its validity something like
You would need to do this for each and every field, and i would actually make a custom class representing the form, populate that class with form fields, and then validate that object in a seperate helper method that way you can reuse the validation regex's etc. Either way if you do it inline or in a method you would then need to process the list of errors something like
Hope that make more sense
Hey Asif, yeah, that makes sense. I was hoping there would be a way to, for instance, fake a 'save' button press in the backoffice from code thereby firing the validation and then using the result of that validation to decide what to do next (process form or show error message), but it was kinda a long shot. Like I said, I don't mind doing things by hand and taking the long way around, but I try not to reinvent the wheel except when i have to!
Thanks for the input, code sample, and your time!
Cheers!
is working on a reply...