none
Validation Problem RRS feed

  • Question

  • I have an access problem with EF and validation in my WPF app. Here's my scenario

    Two tables in my database: TruckType and TruckLoad. Each TruckLoad is linked to one and only one TruckType. TruckTypes can have many related TruckLoads.

    In the TruckType table is a field called LoadLimit.
    In the TruckLoad table is a field called LoadSize.
    LoadSize must not ever be larger than Load Limit.
    This is the validation I'm having problems with.

    My window has a combo box hooked to a ViewSource that lets you select truck types. This is how you navigate around thru the truck type data. The window displays the various TruckType fields on the form for the selected TruckType, including LoadLimit...some of these are editable, others are read-only. The user cannot enter new truck type records from this window, they can only browse and modify some of the fields of existing truck types. Currently LoadLimit is editable but it might be switched to read-only in the future.

    Also in the window is a datagrid which displays all fields of related truckload records from the TruckLoad table which are linked to the truck type selected from the combo box. In this grid is a column linked to the LoadSize field in the TruckLoad table.

    I have been unable to find a location in the source code where validation of the LoadSize entered into the grid has access to the TruckType table in order to determine what the LoadLimit is. Users are allowed to enter new truck load records from the datagrid. When the user moves to the end of the datagrid onto the empty row and starts entering a new TruckLoad record, the TruckLoad Entity passed into the validation function has a null value in its TruckType navigation property...so I can't follow that to find the LoadLimit.

    Currently I'm using a validation rule on the datagrid row that looks like so:

    public partial class TruckLoadValidationRule : ValidationRule
    {
        public override ValidationResult Validate( object value, System.Globalization.CultureInfo cultureInfo)
        {
            TruckLoad tload = (value as BindingGroup).TruckLoads[0] as TruckLoad;

            if( tload.Name .... ) return new ValidationResult( false, "message" );
            if( tload.Cost .... ) return new ValidationResult( false, "message" );

            // This line works fine.
            if( tload.LoadSize < 1 ) return new ValidationResult( false, "Load Size can be no less than 1." );

            // This next line fails because tload.TruckType is null, even though the foreign key field has been correctly entered.
            // Here I have no access to the EF context or any view sources so I cannot perform a query to determine the LoadLimit.
            // Also I do not have access to any of the controls on the window, like the load limit edit box, so I
            // cannot look it up there either. If I try to do this the compiler complains (can't find it) and refuses
            // to generate any object code.
            if( tload.LoadSize > tload.TruckType.LoadLimit) return new ValidationResult( false, "Load Size cannot exceed the truck type's Load Limit." );
            ...
            return ValidationResult.ValidResult;
        }
    }

     XAML:
    <DataGrid.RowValidationRules>
        <my:TruckLoadValidationRule ValidationStep="UpdatedValue" />
    </DataGrid.RowValidationRules>

    My question is: How and where can I validate what the user enters into the LoadSize column on the datagrid such that the related master data's LoadLimit field can be accessed and compared against to determine if the entry is out of range?

     

    • Edited by AxeMurderer Wednesday, September 7, 2011 8:04 AM
    Wednesday, September 7, 2011 6:49 AM

Answers

  • Ok I ended up resolving this by using the datagrid's InitializingNewItem event to explicitly initialize the TruckType navigation property of the new entity by setting it equal to the current item of the TruckTypeViewSource which I do have access to in that event. Not sure if that's a proper thing to do, but it seems to work since there is no null value to contend with in the validation rule later on. I have a feeling its wrong somehow tho and that there's a more proper way to do it.

    Still would appreciate any feedback on the matter.

    Wednesday, September 7, 2011 9:51 AM

All replies

  • Ok I ended up resolving this by using the datagrid's InitializingNewItem event to explicitly initialize the TruckType navigation property of the new entity by setting it equal to the current item of the TruckTypeViewSource which I do have access to in that event. Not sure if that's a proper thing to do, but it seems to work since there is no null value to contend with in the validation rule later on. I have a feeling its wrong somehow tho and that there's a more proper way to do it.

    Still would appreciate any feedback on the matter.

    Wednesday, September 7, 2011 9:51 AM
  • Thank you for sharing your own solution.

     

    Have a nice day,


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, September 9, 2011 7:02 AM
    Moderator