How to use ValidateEntity()?

Posts   
 
    
tedh
User
Posts: 34
Joined: 14-Dec-2006
# Posted on: 15-Dec-2006 21:05:11   

I am a newbie and trying to figure out how to use ValidateEntity(). There is not much documentation or discussion on this forum regarding use of this method. I understand that it can be called on demand.

ValidateEntity() returns a void. So how do I know at the end if the entity is valid or not. Entities don't seem to have an isValid property and ValidateEntity() does not return a boolean for Valid -> true/false.

What I would like to do is to be able to iterate through the fields in an entity and check each one for validity and at the end know if the entity overall is valid or not. I would run ValidateEntity() prior to trying to persist an entity. I have already setup OnValidateFieldValue() so that gui fields are checked for validity as the user exits each individual component.

Thanks

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 16-Dec-2006 12:59:02   

tedh wrote:

I am a newbie and trying to figure out how to use ValidateEntity(). There is not much documentation or discussion on this forum regarding use of this method. I understand that it can be called on demand.

Have you read the Validation section in the manual? Using the generated code -> Validation per field or per entity

The LLBLGenPro reference manual would have shown you that the method isn't used by llblgen pro's internal framework, but you can override it and do validation in there. It's more a backwards compatibility method, as it was present in previous versions, and to not break any code we left it in in v2.0.

ValidateEntity() returns a void. So how do I know at the end if the entity is valid or not. Entities don't seem to have an isValid property and ValidateEntity() does not return a boolean for Valid -> true/false.

What I would like to do is to be able to iterate through the fields in an entity and check each one for validity and at the end know if the entity overall is valid or not. I would run ValidateEntity() prior to trying to persist an entity. I have already setup OnValidateFieldValue() so that gui fields are checked for validity as the user exits each individual component. Thanks

Please check out the documentation section I mentioned above, validation ideas and examples are mentioned there. In short: either use validator objects, or override validation methods in partial classes which are called at various stages of the entity lifecycle.

Frans Bouma | Lead developer LLBLGen Pro
Marko
User
Posts: 2
Joined: 15-Mar-2006
# Posted on: 09-Feb-2007 14:19:22   

I'm having exactly the same problem, after reading the manual?

My questions: 1) How do I know the entity is invalid (see code below)? 2) Rather than throw exceptions can I append error messages and mark the entity invalid? 3) Assuming "Yes" to 2), how do I access the error message?

I'm creating 3-tier remoting app. To minimze chat I want the option to validate entities on client and/or the business layer thru entity.ValidateEntity.

I have a validator object:


Public Class MemberValidator
    Inherits ValidatorBase

    Public Overrides Sub ValidateEntity(ByVal involvedEntity As IEntityCore)
        
        Dim toValidate As MemberEntity = CType(involvedEntity, MemberEntity)

        If toValidate.Fname Is Nothing Then
            Throw New ORMEntityValidationException("Frist name can't be null", toValidate)
        End If

        ...

        MyBase.ValidateEntity(involvedEntity)

    End Sub

End Class

Now suppose the client creates a Member entity and, as below, enters an invalid value, how can I confirm the validity of the entity before communicating with the server? I am heading in this direction:


Dim oMem As New MemberDetails
oMem.Member.Fname = Nothing

Dim memVal As New MemberValidator
Member.Validator = memVal
Member.ValidateEntity()

' Now what? How do I know MemberDetails is invalid...?

BTW, MemberDetails is in a thin wrapper class that holds an an instace of my LLBLB member entity. Don't think this affects matters here?

Am using LLBLG v2.0, VB.Net 2005.

Really appreciate your help here. This is a big brick wall at the moment.

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 09-Feb-2007 15:56:06   

1) How do I know the entity is invalid (see code below)? Public Class MemberValidator Inherits ValidatorBase

Public Overrides Sub ValidateEntity(ByVal involvedEntity As IEntityCore)        
    Dim toValidate As MemberEntity = CType(involvedEntity, MemberEntity)
    If toValidate.Fname Is Nothing Then
        Throw New ORMEntityValidationException("Frist name can't be null", toValidate)
    End If
    ...
    MyBase.ValidateEntity(involvedEntity)
End Sub

End Class

I see you throw an exception in your validation method.

2) Rather than throw exceptions can I append error messages and mark the entity invalid?

The following is copied from the manual: IDataErrorInfo implementation The .NET interface IDataErrorInfo is now implemented on EntityBase and EntityBase2. Two methods have been added to the entities: SetEntityError and SetEntityFieldError, which allows external code to set the error of a field and/or entity. If append is set to true when calling SetEntityFieldError, the error message is appended to an existing message for that field using a semi-colon as separator

3) Assuming "Yes" to 2), how do I access the error message?

Please use the DataErrorInfoError property & the DataErrorInfoErrorsPerField property of the entity. Please check the LLBLGen Pro reference manual.

Posts: 7
Joined: 13-Sep-2006
# Posted on: 27-May-2009 01:05:48   

Has this property DataErrorInfoErrorsPerField implemented in LLBLGen 2.6 Final Release? I try to search for it in the documentation and can not find it.

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 27-May-2009 09:34:22   

It's there and I can find it in the Reference manual.

And the following is written in the Documentation manual:

IDataErrorInfo implementation The .NET interface IDataErrorInfo is now implemented on EntityBase. Two methods have been added to the entities: SetEntityError and SetEntityFieldError, which allows external code to set the error of a field and/or entity. If append is set to true with SetEntityFieldError, the error message is appended to an existing message for that field using a semi-colon as separator.

Entity field validation, which is triggered by the entity's method SetNewFieldValue() (which is called by a property setter), sets the field error if an exception occurs or when the custom field validator fails. The error message is appended to an existing message.

Posts: 7
Joined: 13-Sep-2006
# Posted on: 27-May-2009 17:06:08   

Walaa wrote:

It's there and I can find it in the Reference manual.

And the following is written in the Documentation manual:

IDataErrorInfo implementation The .NET interface IDataErrorInfo is now implemented on EntityBase. Two methods have been added to the entities: SetEntityError and SetEntityFieldError, which allows external code to set the error of a field and/or entity. If append is set to true with SetEntityFieldError, the error message is appended to an existing message for that field using a semi-colon as separator.

Entity field validation, which is triggered by the entity's method SetNewFieldValue() (which is called by a property setter), sets the field error if an exception occurs or when the custom field validator fails. The error message is appended to an existing message.

Yes, I did see this part in the manual. The question I have is can I access **DataErrorInfoError **property & the **DataErrorInfoErrorsPerField **property of the entity directly? How do I extract all the field name from SetEntityFieldError(). Because my form fields are organized into TABs, I need to append the TAB name to each of the field error dscp in the UI layer.

Thanks.

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 27-May-2009 17:24:55   

The DataErrorInfoErrorsPerField field is protected inside the EntityBase class, so you can create a property to expose it inside the generated entityClass.