how to get validation rule error description

how to get validation rule error description

Old forum URL: forums.lhotka.net/forums/t/4476.aspx


pillesoft posted on Friday, March 07, 2008

Dear All,

i'm new to csla and to c# too. using csla 3.0.3.
i've created my first business objects, actually it is also a project object.
it has two smartdate properties. empty values are not allowed.
i've created a validation rule like this:
      ValidationRules.AddRule<Project>(
        IsDateEmpty<Project>, "ProjStart");

      ValidationRules.AddRule<Project>(
        IsDateEmpty<Project>, "ProjFinish");

    private static bool IsDateEmpty<T>(
      T target, Csla.Validation.RuleArgs e) where T : Project
    {
      if (target._projstart.IsEmpty)
      {
        e.Description =
          "Start date can't be empty";
        return false;
      }
      else if (target._projfinish.IsEmpty)
      {
        e.Description =
          "Finish date can't be empty";
        return false;
      }
      else
        return true;
    }

i think it is working fine, however may be not this one is the most efficient solution. any proposal welcome.
at the moment i don't have any GUI, only a console project to test my objects.
here when i save a new project object, which date value is empty i receive a ValidationRule exception, but the description is the default: Object can not be saved, or similar.
i saw in the ProjectTracker example, that the GUI has an errorprovider, which displays the correct validation rule description
but i cannot get the same in my Test console project.
please help me regarding this problem.

Ivan

steve981 replied on Friday, March 07, 2008

Hi, the description of the validation rules broken is in the BrokenRulesCollection property of the business object, I think the following code may help you

     try
    {
      businessObject.Save();
    }
    catch (Csla.Validation.ValidationException ex)
    {
        foreach (Csla.Validation.BrokenRule brokenRule in businessObject.BrokenRulesCollection)
          Console.WriteLine(brokenRule.Property + ": " + brokenRule.Description);
    }

Regards,
Steven Walker

pillesoft replied on Monday, March 10, 2008

thank you Steve, this is exactly what i expected for

Ivan

Copyright (c) Marimer LLC