views:

20

answers:

0

Hi,

I'm trying to validate a nullable datetime using the Enterprise Library Validation Block without success.

I've got a WinForm with a DateTimePicker with a visible checkbox. The DateTimePicker is bound to a nullable DateTime prooperty using a custom databinding where the parsing is done like this (if the DateTimePicker is not checked, the bound DateTime is set to null):

void CheckedDateTimePickerBinding_Parse(object sender, ConvertEventArgs e)
{
    Binding b = sender as Binding;
    if (b == null)
        return;

    DateTimePicker dtp = (b.Control as DateTimePicker);
    if (dtp == null)
        return;

    if (dtp.Checked == false)
    {
        e.Value = null;
    }
    else
    {
        e.Value = dtp.Value.Date;
    }
}

To the form I have added an ErrorProvider and a ValidationProvider. The ValidationProvider is set to work together with the ErrorProvider:

this.validationProvider1.ErrorProvider = this.errorProvider1;
this.validationProvider1.RulesetName = "MyRuleSet";
this.validationProvider1.SourceTypeName = "ValidationTest.MyClass, ValidationTest";
this.validationProvider1.ValidationResultFormat = "{2} {0}";

The ValidationProvider is configured to validate the DateTimePicker:

this.validationProvider1.SetPerformValidation(this.excersiceDateTimePicker, true);
this.validationProvider1.SetSourcePropertyName(this.excersiceDateTimePicker, "ExerciseDate");
this.validationProvider1.SetValidatedProperty(this.excersiceDateTimePicker, "Value");

Other controls, such as TextBoxes and Comboboxes work fine with the same set up. The only difference is that those control do not have any custom databinding.

The validation check I have defined is to check if the DateTime is null using the NotNullValidator. The validation rules are defined in a .config-file used by both the client for early input validation and by the server to validate before saving changes to the database. The entity classes are generated, so using the attributes is not an option.

The problem is that the validation never fails; even if the DateTimePicker is not checked the ErrorProvider does not display any red !-icon!

My unit tests work fine. If I set the DateTime property to null, then the validation check returns a ValidationResults with IsValid set to false. Also, on the server side it works fine. Both the unit tests and on the server side the big difference is that they do not use the WinForm Validator integration.

The work around I'm currently using is to validate the DateTimePicker using the common Validating/Validated events, but that means the validation rule is defined in two places, both in the handlier for the Validating event and in the config-file. I would like to have all validation rules in one place only!

What I believe is happening is that the Enterprise Validation Block's WinForm integration somehow skips the custom databinding.

Any ideas what I'm doing wrong?