Hi peeps, I have been messing around with some code and have alot of validation that is duplicating itself such as -
Client Side:
// Start Validation - Client Side
// Validate userName
Validation.RequireField("userName", "You must specify a User Name");
Validation.Add("userName", Validator.StringLength(maxLength: 20, minLength: 0, errorMessage: "User Name must be between 1 and 20 characters long"));
Validation.Add("userName", Validator.Regex(@"^[a-zA-Z0-9]+$", "User Name can only contain letters and numbers (no spaces)"));
and then server side:
// Start Validation - Server Side
// Validate userName
if (userName.IsEmpty())
{
ModelState.AddError("userName", "You must specify a User Name");
}
if (!ValFunctions.IsAlphaNumeric(userName))
{
ModelState.AddError("userName", "User Name can only contain letters and numbers (no spaces)");
}
if (ValFunctions.IsGreaterThanOrEqualTo(userName.Length, 21))
{
ModelState.AddError("userName", "User Name must be between 1 and 20 characters long");
}
now these two snippets as such are used across at least 4-5 pages in the site. Should I take them out and seperate from the main code? If so can anyone help? not sure how I would go about this?
For the past few months I have created a website from scratch.
At every turn I have asked myself a few questions, whenever I encountered something that would be an improvement over the existing code.
1. Does this change improve the user experience?
2. How much time will it take me to implement?
3. Can I use that time for something else that is beneficial for the user experience?
4. Will it in any way a change in load times?
5. Will the change affect the maintainability or readability when I need to go back through this code a year from now and figure out how it exactly works?
You seem to have a significant misinderstanding of how the Validation helpers work. For example you have this section of code labelled "Start validation - client side":
Tuppers
Validation.RequireField("userName", "You must specify a User Name");
That actually works both on the client and the server. Accordingly, there is no need whatsoever for this:
Tuppers
if (userName.IsEmpty())
{
ModelState.AddError("userName", "You must specify a User Name");
}
The validation helper does that for you.
Equally, there is no need for your Valfunctions.IsAlphaNumeric function. The Regex validator does that for you both on the client and the server. Basically, if you are using the Validation helpers, you should not be referencing ModelState at all.
Well I never! Thanks alot Mike!! Got a little confused with the two books I am reading atm! (one of which is yours!!)
So I still have the following code block that I use over 4-5 pages:
// Validate userName
Validation.RequireField("userName", "You must specify a User Name");
Validation.Add("userName", Validator.StringLength(maxLength: 20, minLength: 0, errorMessage: "User Name must be between 1 and 20 characters long"));
Validation.Add("userName", Validator.Regex(@"^[a-zA-Z0-9]+$", "User Name can only contain letters and numbers (no spaces)"));
do I create a file to keep DRY or just leave it? I have lost a shed load of code! lol!
If it was me, I'd just leave it as it is. Otherwise you will end up trying to fit all forms in one file and adding a load of complexity to include and exclude fields and validation as needed. That may well help to achieve DRY, but will drive a coach and
horses through KISS. KISS beats DRY every time in my book (well, not necessarily the one you are currently reading, but you get what I mean....)
Tuppers
Member
455 Points
232 Posts
Not sure how to remain DRY here??
Jan 03, 2013 02:48 PM|LINK
Hi peeps, I have been messing around with some code and have alot of validation that is duplicating itself such as -
Client Side:
// Start Validation - Client Side // Validate userName Validation.RequireField("userName", "You must specify a User Name"); Validation.Add("userName", Validator.StringLength(maxLength: 20, minLength: 0, errorMessage: "User Name must be between 1 and 20 characters long")); Validation.Add("userName", Validator.Regex(@"^[a-zA-Z0-9]+$", "User Name can only contain letters and numbers (no spaces)"));and then server side:
// Start Validation - Server Side // Validate userName if (userName.IsEmpty()) { ModelState.AddError("userName", "You must specify a User Name"); } if (!ValFunctions.IsAlphaNumeric(userName)) { ModelState.AddError("userName", "User Name can only contain letters and numbers (no spaces)"); } if (ValFunctions.IsGreaterThanOrEqualTo(userName.Length, 21)) { ModelState.AddError("userName", "User Name must be between 1 and 20 characters long"); }now these two snippets as such are used across at least 4-5 pages in the site. Should I take them out and seperate from the main code? If so can anyone help? not sure how I would go about this?
Thanks in advance
Tuppers
wavemaster
Participant
1279 Points
1127 Posts
Re: Not sure how to remain DRY here??
Jan 03, 2013 02:56 PM|LINK
Re-using code typically is done through a function, but these Validation helpers in itself are already functions.
Tuppers
Member
455 Points
232 Posts
Re: Not sure how to remain DRY here??
Jan 03, 2013 05:23 PM|LINK
I know thats whats confusing me, should I just keep the same code blocks on the pages as they already reference functions to complete tasks.
wavemaster
Participant
1279 Points
1127 Posts
Re: Not sure how to remain DRY here??
Jan 03, 2013 05:28 PM|LINK
For the past few months I have created a website from scratch.
At every turn I have asked myself a few questions, whenever I encountered something that would be an improvement over the existing code.
1. Does this change improve the user experience?
2. How much time will it take me to implement?
3. Can I use that time for something else that is beneficial for the user experience?
4. Will it in any way a change in load times?
5. Will the change affect the maintainability or readability when I need to go back through this code a year from now and figure out how it exactly works?
Mikesdotnett...
All-Star
154901 Points
19864 Posts
Moderator
MVP
Re: Not sure how to remain DRY here??
Jan 03, 2013 07:54 PM|LINK
You seem to have a significant misinderstanding of how the Validation helpers work. For example you have this section of code labelled "Start validation - client side":
That actually works both on the client and the server. Accordingly, there is no need whatsoever for this:
The validation helper does that for you.
Equally, there is no need for your Valfunctions.IsAlphaNumeric function. The Regex validator does that for you both on the client and the server. Basically, if you are using the Validation helpers, you should not be referencing ModelState at all.
See this article for more details: http://www.mikesdotnetting.com/Article/191/Validation-In-Razor-Web-Pages-2
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
Tuppers
Member
455 Points
232 Posts
Re: Not sure how to remain DRY here??
Jan 04, 2013 05:37 PM|LINK
Well I never! Thanks alot Mike!! Got a little confused with the two books I am reading atm! (one of which is yours!!)
So I still have the following code block that I use over 4-5 pages:
// Validate userName Validation.RequireField("userName", "You must specify a User Name"); Validation.Add("userName", Validator.StringLength(maxLength: 20, minLength: 0, errorMessage: "User Name must be between 1 and 20 characters long")); Validation.Add("userName", Validator.Regex(@"^[a-zA-Z0-9]+$", "User Name can only contain letters and numbers (no spaces)"));do I create a file to keep DRY or just leave it? I have lost a shed load of code! lol!
Thanks Tuppers
Mikesdotnett...
All-Star
154901 Points
19864 Posts
Moderator
MVP
Re: Not sure how to remain DRY here??
Jan 04, 2013 06:03 PM|LINK
If it was me, I'd just leave it as it is. Otherwise you will end up trying to fit all forms in one file and adding a load of complexity to include and exclude fields and validation as needed. That may well help to achieve DRY, but will drive a coach and horses through KISS. KISS beats DRY every time in my book (well, not necessarily the one you are currently reading, but you get what I mean....)
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
goel.ankit
Contributor
2531 Points
513 Posts
Re: Not sure how to remain DRY here??
Jan 08, 2013 12:30 PM|LINK
You can create custom validation attribute and decorate your model with the property.
Sample ->
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)] public class CustomMinLengthAttribute : ValidationAttribute { public int MinValue { get; set; } public override bool IsValid(object value) { return value==null || (value.ToString().Trim()).Length >= MinValue; } }Ankit
(Please select 'Mark as Answer' if my response has helped you.)