Validation

Posts   
 
    
BISHMAN
User
Posts: 41
Joined: 30-Jan-2010
# Posted on: 21-Feb-2010 18:29:25   

I am very new to LLBLGen. I am trying to find out how to impliment my own validation. I have found the sample on your web site and the order part of it is exactly what I want to do. My problem is that I do not know 'C' and I seam to be doing something wrong when I try to convert the code to visual basic. Is there a good example of validation written in VB? Beter yet, has the example that you provide been rewritten in VB?

I guess I am most confused with the GetEntityFieldsErrors and ResetErrors areas of the example. And more specifically the following code: private void ResetErrors() { // current item OrderEntity entityToReseterrors = (OrderEntity)_ordersBinder.Current;

        // reset the field errors
        foreach (EntityField field in entityToReseterrors.Fields)
        {
            entityToReseterrors.SetEntityFieldError(field.Name, string.Empty, false);
        }
        // reset entity error
        entityToReseterrors.SetEntityError(string.Empty);
    }

and

    private string GetEntityFieldsErrors()
    {
        // current item
        OrderEntity currentItem = (OrderEntity)_ordersBinder.Current;

        // variables to construct the message
        StringBuilder sbErrors = new StringBuilder();
        string toReturn = string.Empty;

        // iterate over fields and get their errorInfo
        foreach (IEntityField field in currentItem.Fields)
        {
            /// IEntity implements IDataErrorInfo, and it contains a collections of field errors already set. 
            /// For more info read the docs (LLBLGen Pro Help -> Using generated code -> Validation per field or per entity -> IDataErrorInfo implementation).
            if (!string.IsNullOrEmpty(((System.ComponentModel.IDataErrorInfo)currentItem)[field.Name]))
            {
                sbErrors.Append(((System.ComponentModel.IDataErrorInfo)currentItem)[field.Name] + ";");
            }
        }

        // determine if there was errors and cut off the extra ';'
        if (sbErrors.ToString() != string.Empty)
        {
            toReturn = sbErrors.ToString();
            toReturn = toReturn.Substring(0, toReturn.Length - 2);
        }

        return toReturn;
    }

How are these done in VB?

Again I am just getting started and any help would be greatly appreciated.

BISHMAN
User
Posts: 41
Joined: 30-Jan-2010
# Posted on: 21-Feb-2010 18:32:11   

Sorry, I am using V2.6 final, selfserving.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 22-Feb-2010 03:50:35   

Hi there,

There is no version of that example written in VB. That two methods are helper of the auditor. One of them cleans the erros in the entity (ResetErros) and the other get back the erros in a GUI friendly way, so you can use them in your GUI.

The statements are very straignt forward and esasily converible to VB. Please refer to a C#-VB converter or try it yourself and let us know if you have conercs or issues with some lines.

Regard.

David Elizondo | LLBLGen Support Team
BISHMAN
User
Posts: 41
Joined: 30-Jan-2010
# Posted on: 22-Feb-2010 17:31:15   

Thanks for the help. I'm still missing something. In my project I have a screen that displays all of the records from the glgroup table in a datagridview. The user can click on one the rows which brings up an edit screen for the selected item. The user can then make changes to that record and save which returns them back to the selection screen. When selecting an existing record my validation works just fine on the edit screen. ie. if a field is to long or empty I get my errorprovider exclaimation point. The problem I am having is when the add button is selected. It display a blank edit screen which it should but if I type inappropriate data into one of the fields and tab off of the field I do not get my errorprovider notification. What am I missing?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 23-Feb-2010 02:29:41   
  • It must be someting on how your Add-New screen is binding to the collection, it should bind to a instance of your entity. What type of controls are you using?

What is the logic behind that Add-New screen?

Are you using LLBLGenProDataSource for the binding? (show us how this is done)

David Elizondo | LLBLGen Support Team
BISHMAN
User
Posts: 41
Joined: 30-Jan-2010
# Posted on: 23-Feb-2010 14:50:09   

The logic in the calling form is as follows: Private Sub AddNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddNewItem.Click Dim editfrm As New GL_GroupEdit editfrm.LoadGroup(9999) editfrm.ShowDialog(Me)

    If editfrm.cancelClicked Then
        Return
    End If

    resetGrid()

The edit form has two text boxes, they are both bound to a glgroupcollection. The LoadGroup logic looks like this:

Friend Sub LoadGroup(ByVal GroupNumber As Int16)
    Dim filter As New PredicateExpression(GlgroupFields.GroupId = GroupNumber)
    GlgroupCollection1.GetMulti(filter)
End Sub

It also has a save and a cancel button. The save logic looks like this:

Private Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click
    cancelClicked = False

    Dim groupToSave As GlgroupEntity = DirectCast(GLGroupBindingSource.Current, GlgroupEntity)

    If GetEntityFieldsErrors() <> String.Empty Then
        MessageBox.Show("There are errors in the group. Please fix them prior to save.", "Please fix the errors.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Exit Sub
    Else
        Try
            groupToSave.Save(True)
        Catch ex As ORMEntityValidationException
            MessageBox.Show(ex.Message)
            Exit Sub
        End Try
    End If

    Me.Close()
End Sub

Here's the GetEntityFieldsErrors logic:

Private Function GetEntityFieldsErrors() As String
    Dim currentItem As GlgroupEntity = DirectCast(GLGroupBindingSource.Current, GlgroupEntity)
    Dim sbErrors As New StringBuilder
    Dim toReturn As String = String.Empty

    For Each field As IEntityField In currentItem.Fields
        If Not String.IsNullOrEmpty(DirectCast(currentItem, System.ComponentModel.IDataErrorInfo)(field.Name)) Then
            sbErrors.Append(DirectCast(currentItem, System.ComponentModel.IDataErrorInfo)(field.Name) & ";")
        End If
    Next

    If sbErrors.ToString() <> String.Empty Then
        toReturn = sbErrors.ToString()
        toReturn = toReturn.Substring(0, toReturn.Length - 2)
    End If

    Return (toReturn)

End Function
MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 23-Feb-2010 21:22:13   

What's the 9999 doing when you AddNew? Don't you want to create a new instance of the Group entity and let the user edit that ?

Matt

BISHMAN
User
Posts: 41
Joined: 30-Jan-2010
# Posted on: 23-Feb-2010 21:53:55   

The group table simply contains two fields, the groupid and groupname. The groupid is the pk. Unfortunately there is a record number 0 in the table. In the logic I sent before you will see that I open the edit screen by passing the groupid. The 9999 is to ensure that it will not find a record with that number. Should I be doing this differently?

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 23-Feb-2010 22:49:18   

Yes, you shouldn't be using Magic Numbers - they are generally considered a bad thing simple_smile

So when you try and load the entity with the Id 9999, what are you getting back - a null, or a new entity ? You need to ensure that if Id=9999 you make sure that you create a new instance of the group entity for the user to edit.

Matt