Using DataAnnotations to validate DynamicContent fields

Vote:
 

I have created a simple DynamicContent plugin by decorating my usercontrol with the DynamicContentPlugIn-attribute, and my public properties are automatically exposed to the editor as approprioate input boxes (depending on each property's type), as expected. SO far so good. Now I want to extent the input validation.

I want my property to be required, and I want to limit the range of valid integers that can be entered by the editor.

Is there a simple way of doing this? I was hoping for DataAnnotations-support, as described in this blog post for developing Visitor Criterias (http://world.episerver.com/Documentation/Items/Tech-Notes/EPiServer-CMS-6/EPiServer-CMS-6-R2/Visitor-Groups-Creating-Custom-Criteria/), but that does not seem to work..

Am I really forced to write a while separate edit-usercontrol that inherits from DynamicContentEditControl, just to implement this simple task (as examplified in this post by Fredrik Haglund: http://blog.fredrikhaglund.se/blog/2011/06/28/dynamic-content-in-episerver-cms-6-r2/)?

Please tell me I am missing something.. :-)

DynamicContentPlugIn
#53057
Aug 25, 2011 22:49
Vote:
 

Unfortunately I don't think that it's anything else but visitor groups that support data annotations for validation at the moment, unless I have missed something. We are currently working to improve this to allow data annotations as a simpler way of defining the requirements for your properties.

Regads
Linus Ekström
EPiServer Development Team 

#53059
Aug 26, 2011 7:20
Vote:
 

I wrote a short post about using data annotations for validating a page upon save here. Perhaps that's something that can be of use?

#53060
Aug 26, 2011 7:47
Vote:
 

Hi,

I ended up creating my own edit-usercontrol, with the custom controls provided by the EPIServer Template Foundation project on Codeplex (http://etf.codeplex.com/). After modifying their controls by decorating the classes with [ValidationProperty("propertyname")], I could use standard asp.net-validators to validate their values in my edit-usercontrol. So far so good.

Now I face the problem of accessing the properties exposed by my view-usercontrol from codebehind in my edit-usercontrol. I just can't find a way of reaching them with strongly typed code!

What I tried was this:

((MyViewControl)Content).Width = txtHeight.Text;

But this caused a runtime error, because "Content" wasn't of the correct type. It was of type DynamicContentAdapter<MyViewControl>.

So instead, had to write with code that looks a few years old, with the name of the property in quotes (*shudder*):

Content.Properties["Width"].Value = txtHeight.Text;

Can anyone see what I am doing wrong? How can I access the properties directly through the Content-property exposed by DynamicContentEditControl?

-bjørn

#53156
Aug 30, 2011 10:55
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.