This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
With the changes in the latest nightly 04/04/2011, clientside validation with Lightspeed entities is quite straightforward.
Create this class somewhere: public class LightspeedModelValidatorProvider : DataAnnotationsModelValidatorProvider
Then in Application_Start() do the following: protected void Application_Start()
This will create the following attributes as required: DA.RequiredAttribute();
You can add additional attributes in the "Custom Attributes" field in the entity properties. Just add the full Namespace so you don't get compile errors. E.g. System.ComponentModel.DataAnnotations.Display(...)
HTH Sean
|
|
|
Thanks for posting this Sean! As a bonus tip, if you get tired of typing System.ComponentModel.DataAnnotations in the entity property custom attributes section, you can import the System.ComponentModel.DataAnnotations namespace into the generated code: go to the LightSpeed Model Explorer, right-click the root model node and choose "Add New Imported Namespace". You'll then be able to use attributes from this namespace without qualification! |
|
|
Since the System.ComponentModel.DataAnnotations attributes are getting more and more the MS standard way (WPF, SL, MVC), can we get these also supported directly within the designer? Beside the validation attributes I also would like to get:
Regards, Sörnt
|
|
|
OK, I have been using the "old" manual method from MVC 1.0, and thought this was going to be great! No more specifying the Data Attributes manually on teh client. Except, it doesn't work for me... I am using a WCF service to access the database and ship Entities back and forth to the client. I am NOT using the Distributed Unit of Work model, so I just get collections of entities at the client. When I puut a breakpoint in teh GetValidators method above, I see either 4 or 6 members in the newAttributes array, here is one with 6: System.Runtime.Serialization.DataMemberAttribute System.Runtime.InteropServices.ComVisibleAttribute System.Reflection.DefaultMemberAttribute System.CLSCompliantAttribute System.Runtime.CompilerServices.TypeDependencyAttribute System.SerializableAttribute
The field is declared like this on the server side (Model.cs): [ValidatePresence] [ValidateLength(0, 16)] private string _shortName; |
|
|
Anybody? Does this only work if I am using the Distributed Unit of Work? It seems straightforward, as SeanX claims, but it does nothing for me. What's the "trick"?
Dave |
|
|
Hi Dave, I think Seans example was specific to using MVC and a local unit of work, if you are able to send through a quick sample of this I can have a poke and see if I can work out whats going on and whats missing.
Jeremy |
|
|
I'll see what I can do. A "quick" sample of a client-server application is not so quick to come up with, but I should be able to do something tomorrow, assuming I don't get pulled in 20 different directions by other things. I may just decide it's not worth it at this time and soldier on without it, but I would really like to get it working... Dave |
|
|
OK, so I looked at my client code adn decided to get rid of the ServiceReferences and use Channels instead. What a mess! There doesn't seem to be any one right way to do it, and to top that off, there seems to be something wrong with just about every way people have tried to use them. When I get this figured out I will upload a quick demo. Until then, I am learning new ways to confound myself... Dave |
|
|
OK, assuming I can figure out how to attach a ZIP file to this message, here is an example of what I'm trying to do. You can generate the database from the model, but for some reason the Identity column doesn't get set to generate the ID value, so you will have to tweak the database to do that -- otherwise it complains about stuffing a NULL value into the identity column... Run the service (startup program, DemoConsoleHost) then start the MVC program (MvcValidationDemo). Click on the tab that says "Click ME!!" to get to the index page for ParentTypes. There is a Presence and Length validation (<= 15) on teh only field there. Thanks for looking. Like I said, any client=server sample is not quick or easy.... :) Dave |
|
|
Jeremy - Have you had a chance to look at the sample code I posted here? Can you get it to work on your end? I would appreciate any feedback, even if it's "I'm busy right now, I'll get to it in a couple of days"... Thanks, Dave |
|
|
Hi Dave, Am having a look at this now, will keep you posted on what I can find.
Jeremy |
|
|
Hi Dave, Just fired it up, and I noticed that you had not added in Sean's code for this sample. After adding this in the ValidatorProvider class triggered the DataAnnotation attributes for presence and length. Presumably this doesnt match up with what you were seeing earlier? In which case I would have a look at what the differences are between the sample and your actual code. Some initial thoughts would be:
Jeremy
|
|
|
Hi Jeremy - My apologies for not adding the crucial piece of code to make the sample work! Oops... After confirming your findings here, I went back and converted my application from using Service References to using Channels (it took all day to make all the necessary changes), and all started working as it should. Apparently there is not enough information shared via the WSDL to make the validations work. But, once I added a reference to the actual model, of course all is well with the world once more. I don't know if I could eventually make it work with Service References, but I don't have a need to as I control both ends of the communication channel. Great stuff!
Thanks for the help, Dave |
|
|
Hi Dave, Yes that makes sense and glad to hear its all sorted. The way the DataAnnotation provider works is by inspecting the fields to determine the validation attributes required. That doesnt actually form part of the contract as specified in the WSDL so any generated proxy classes omit that sort of information which is why you were not seeing them and why the DataAnnotation provider was effectively seeing nothing :) Assuming its possible you should just share the model classes, you are taking a reference on LightSpeed anyway to get the annotation provider and are controlling both ends so that seems fairly reasonable.
Jeremy |
|