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
|
Hello, Is there a way to override the built-in validation for an entity? Here is the issue. I have a Template Entity with 2 Entity Properties - Name and Description out of which the Name is required. I also have a TemplateProperties Entity which has a one to many relationship with the Template Entity ( One Template can have many TemplateProperties) So in my code when I create a new Template by saying Template x = new Template() and I don't specify the name the template is invalid but in addition to that the TemplateProperty automatically becomes invalid as well. So if the user omits to type the name in the template form, instead of just getting a message saying that Template Name is required, the user gets a message saying Template Name is required and then say I have 3 properties I would also get : Property[0] is invalid Property[1] is invalid Property[2] is invalid So I was thinking of overriding the validation of the TemplateProperty to get around this issue so that even if the underlying template is invalid, the property still is valid since the template has its own validation. How can I do that? What other ways would u recommend for getting around the issue?
Also instead pf getting a message saying Property[0] is invalid I would like to get a more meaningful message saying Property with Name XYZ is invalid for example. How can I do that?
Thanks very much in advance
Susan
|
|
|
I should probably refraze my first question a bit. How can I override the OnValidate() methos of an entity so that the entity is always valid - that would be the first step. After that I will figure out how to do the custom validation. Right now if I leave the method OnValidate blank the built-in validation is still being executed and I want to avoid that.
Susan |
|
|
There isn't a way to suppress the built-in validation through OnValidate. If you don't want LightSpeed to perform automatic validations, just don't apply the validation attributes (or turn off the validation settings in the designer). However it sounds like your problem is not so much that the validations on the entity itself are unwanted, but that you don't want validation errors on *associated* entities showing up on the "main" entity. Is that the issue? |
|
|
Yes - exactly. I need to avoid that. Also the other issue is that I want to be able to change the message that comes up when there is a validation error. Thanks Susan |
|
|
We don't currently support that directly, but you can achieve the same effect by filtering the Errors collection. A validation error applies to the main object if its ChildErrors collection is empty, and to an associated object if its ChildErrors collection is non-empty. So to display only the errors on the entity itself, use something like this: var errorsToDisplay = entity.Errors.Where(e => e.ChildErrors.Count == 0); Will that meet your requirements? To change the message that comes up when there is a validation error, set the Message property in the validation attribute. If you are using the designer, you can't do this directly on the entity, you instead need to select the validation itself. To do this, go to the LightSpeed Model Explorer window (View > Other Windows > LightSpeed Model if it's not already open), expand the entity, expand the Validations collection, and select the validation whose message you want to modify. You can now enter the custom message via the Properties window. Note you cannot change the message for "an associated entity is invalid" errors, but since you wish to filter them out, this shouldn't be an issue I think. |
|
|
Thanks for the reply, The suggestion above would work in a simple scenario but in my case I can not use it. I have a Template entity which has a one to many reference to a Property entity and thus has a Properties collection. So let's say the first property is invalid - the built in validation of the Template kicks in and checks each of its properties - this functionality I like. So I get a message saying Property 0 is invalid There are two issues with that: 1) I do not get the message from the Property 0 Errors collection describing what exactly is wrong. I just get Property 0 is Invalid. Keep in mind that I have disabled the built-in validation on the property itself and implemented my own. I did that because the property has a reference back to the template so if I had an invalid template then I was getting a false positive saying that the property is also invalid since it was checking the properties child Errors and based on that determining that the property is also invalid. 2) Instead of saying Property 0 I would like to have the property name display. For example Lenght or Weight. How can I achieve that?
Thanks
Susan |
|
|
Regarding 1, to find out why Property 0 is invalid, check the ValidationError.ChildErrors collection. This contains all the validation errors on the associated Property entity so you can use this to find exactly what it wrong. Regarding 2, you can't customise the message generated by LightSpeed. However I have now committed a change which will allow you to get at the associated entity (the entity in the child collection to which the "invalid associated object" error refers) so you can use this to construct your own error message. So you should now be able to get the detailed error report something like this: void PrintErrorMessages(Template template) { The ValidationError.AssociatedEntity property will be available in nightly builds dated 6 June 2009 and above, available from about 1430 GMT. Hope this addresses the problem for you! |
|