I have a view in which the user can edit a collection of "heterogeneous" items, each of the item currently is implemented as an instance of class "WAttribute", for example:
public class WAttributeViewModel {
public string DisplayName;
public string UserValue;
... other "meta" data properties describing the WAttribute, such as format string (0.00%), minValue, maxValue...
}
In the controller, I will get a list of this WAttributeViewModel as List<WAttributeViewModel> from the database, the displayname will be shown as the Label in the view, UserValue will be a text input, and the user can modify the UserValue, then I will save
the UserValues back to the database, when the user hit the "Save" button/link.
The problem is: different rows of WAttributeViewModel in the list can have different validation reqirements, for example:
1. for a "Field Percentage" WAttribute, it is a percentage, it should be shown as x.yz%, and I should validate its UserValue as numeric percentage (and different percentage WAttribute may have different min/max range requirements)
2. for a "Facility Location" WAttribute, it is a string, it should be shown as string, and I don't need to validate its value, i.e. the UserValue can be empty.
3. for a "Project Class" WAttribute, it is a string, and its UserValue
cannot be empty.
4. for a "Installation Date" WAttribute, it is a date, it should be shown as MM-DD-YYYY, and I need to validate its value, ie. it is a date and cannot be empty.
5. the list goes on and on, and each row in the list may have different requirements for validation, these requirements are stored in the database and I can get them into the WAttributeViewModel.
So, I think I cannot use DataAnnoation attributes, such as [Required], or [Range], to descorate the UserValue property in my WAttributeViewModel, because each row/instance will have different requirements for validation, some are [Required] some are not,
some have a range of [1..10], and some have [100..10000].
And I think I cannot add/modify an (DataAnnoation) attribute of a property at run time.
So, what is the best way to implement validation for my case?
Before using MVC, we used ASP.NET web forms, and we used telerik's RadInputManager, and we create multiple inputsettings to manage the vlidation and formating of the values. Is there a similar control/method that i can use in the MVC world?
you can use
IValidatableObject Interface , look at this
Example , or you can create custom Vaidation attribute and and using reflection you can check the values .
I proposed to define a BrokerAttribute plus the normal validation attributes. Probably with minor modifications the same approach might be viable also for your case.
Thanks Ahmecl, I have read the example in your link, seems it can solve my problem, although I am a little uncomfortable writing DAL code in the model class, but I will give it a try.
Thanks for the reply, but I don't really understand your reply to the other thread, do you have a more complete example / blog that I can read into more details?
Sorry I don't have a working example, since in my software I try to avoid such a metaclasses that are diffcult to mantain. I prefer using strongly typed classses+some dependency injection+interfaces to make they "open" to new types.
However the basic idea is simple, at leasdt for the server side validation (it is better you start from there, since it is easier).
You implement a custom validationa attribute that just specified the name of another property whose value would be the actual validation attribute to apply. Let make an examle
[Broker("ValidationType", ValidationArguments)]
public string UserValue{get; set;)
public Type ValidationType {get; set;}
public object[] ValidationArguments {get; set}
Now since the property ValidationType can accept a Type you can give it the type of a validation attribute as value:
x.ValidationType=typeof(RequiredAttribute);....
x.ValidationArgument = new object[0];
Now when you write the code of the BrokerAttribute. In its IsValid method. you just access the ValidationType property that is passed as parameter of the BrokerAttribute, and via reflection you invoke its IsValid Method. You use the ValidationArgument to
pass some parameters to the valòidation attribute
This way you can implement a "variable" validation, whose validation rules are specified in the properties of the classes of your list.
WenbiaoLiang
Member
2 Points
16 Posts
Custom Validation on Collection of heterogeneous items
May 08, 2012 04:30 PM|LINK
Hi guys,
I have a view in which the user can edit a collection of "heterogeneous" items, each of the item currently is implemented as an instance of class "WAttribute", for example:
public class WAttributeViewModel {
public string DisplayName;
public string UserValue;
... other "meta" data properties describing the WAttribute, such as format string (0.00%), minValue, maxValue...
}
In the controller, I will get a list of this WAttributeViewModel as List<WAttributeViewModel> from the database, the displayname will be shown as the Label in the view, UserValue will be a text input, and the user can modify the UserValue, then I will save the UserValues back to the database, when the user hit the "Save" button/link.
The problem is: different rows of WAttributeViewModel in the list can have different validation reqirements, for example:
1. for a "Field Percentage" WAttribute, it is a percentage, it should be shown as x.yz%, and I should validate its UserValue as numeric percentage (and different percentage WAttribute may have different min/max range requirements)
2. for a "Facility Location" WAttribute, it is a string, it should be shown as string, and I don't need to validate its value, i.e. the UserValue can be empty.
3. for a "Project Class" WAttribute, it is a string, and its UserValue cannot be empty.
4. for a "Installation Date" WAttribute, it is a date, it should be shown as MM-DD-YYYY, and I need to validate its value, ie. it is a date and cannot be empty.
5. the list goes on and on, and each row in the list may have different requirements for validation, these requirements are stored in the database and I can get them into the WAttributeViewModel.
So, I think I cannot use DataAnnoation attributes, such as [Required], or [Range], to descorate the UserValue property in my WAttributeViewModel, because each row/instance will have different requirements for validation, some are [Required] some are not, some have a range of [1..10], and some have [100..10000].
And I think I cannot add/modify an (DataAnnoation) attribute of a property at run time.
So, what is the best way to implement validation for my case?
Before using MVC, we used ASP.NET web forms, and we used telerik's RadInputManager, and we create multiple inputsettings to manage the vlidation and formating of the values. Is there a similar control/method that i can use in the MVC world?
Thanks!
Wenbiao
Ahmed Moosa
Star
7784 Points
1232 Posts
Re: Custom Validation on Collection of heterogeneous items
May 08, 2012 04:36 PM|LINK
Hi,
you can use IValidatableObject Interface , look at this Example , or you can create custom Vaidation attribute and and using reflection you can check the values .
hope this helps
MCC - MCPD -MCTS
francesco ab...
All-Star
20888 Points
3277 Posts
Re: Custom Validation on Collection of heterogeneous items
May 08, 2012 04:51 PM|LINK
Give a look to the answer I gave to a similar question: http://forums.asp.net/t/1703025.aspx/1?MVC3+data+driven+form+Unobtrusive+Validation+without+Data+Annotations
I proposed to define a BrokerAttribute plus the normal validation attributes. Probably with minor modifications the same approach might be viable also for your case.
Mvc Controls Toolkit | Data Moving Plug-in Videos
WenbiaoLiang
Member
2 Points
16 Posts
Re: Custom Validation on Collection of heterogeneous items
May 08, 2012 07:42 PM|LINK
Thanks Ahmecl, I have read the example in your link, seems it can solve my problem, although I am a little uncomfortable writing DAL code in the model class, but I will give it a try.
Thanks again!
WenbiaoLiang
Member
2 Points
16 Posts
Re: Custom Validation on Collection of heterogeneous items
May 08, 2012 07:43 PM|LINK
Hi Francesco,
Thanks for the reply, but I don't really understand your reply to the other thread, do you have a more complete example / blog that I can read into more details?
Thanks again!
francesco ab...
All-Star
20888 Points
3277 Posts
Re: Custom Validation on Collection of heterogeneous items
May 09, 2012 02:02 PM|LINK
Sorry I don't have a working example, since in my software I try to avoid such a metaclasses that are diffcult to mantain. I prefer using strongly typed classses+some dependency injection+interfaces to make they "open" to new types.
However the basic idea is simple, at leasdt for the server side validation (it is better you start from there, since it is easier).
You implement a custom validationa attribute that just specified the name of another property whose value would be the actual validation attribute to apply. Let make an examle
[Broker("ValidationType", ValidationArguments)]
public string UserValue{get; set;)
public Type ValidationType {get; set;}
public object[] ValidationArguments {get; set}
Now since the property ValidationType can accept a Type you can give it the type of a validation attribute as value:
x.ValidationType=typeof(RequiredAttribute);....
x.ValidationArgument = new object[0];
Now when you write the code of the BrokerAttribute. In its IsValid method. you just access the ValidationType property that is passed as parameter of the BrokerAttribute, and via reflection you invoke its IsValid Method. You use the ValidationArgument to pass some parameters to the valòidation attribute
This way you can implement a "variable" validation, whose validation rules are specified in the properties of the classes of your list.
Mvc Controls Toolkit | Data Moving Plug-in Videos
WenbiaoLiang
Member
2 Points
16 Posts
Re: Custom Validation on Collection of heterogeneous items
May 09, 2012 08:05 PM|LINK
Francesco,
Thanks a lot for the detailed message, I appreciate it. I will give it a good read. :)
Thanks again,