cslaforum icon indicating copy to clipboard operation
cslaforum copied to clipboard

How do I get validation error information in the sublist

Open angtianqiang opened this issue 6 years ago • 5 comments

@rockfordlhotka Sublists verification does not pass, but the ROOT class of the Entity. The BrokenRulesCollection. Count is 0, Csla. Rules. BusinessRules. GetAllBrokenRules method returns an empty, IsValid=false for the ROOT class. In CSLA4.7.2, it is possible to return error messages for sublists. What should I do in the new version 5.0.1?

angtianqiang avatar Dec 13 '19 05:12 angtianqiang

That seems like a bug

angtianqiang avatar Dec 13 '19 05:12 angtianqiang

It is possible that there's a bug. I looked, and the code for GetAllBrokenRules was written by someone (maybe me?) before we started using git, so the history would take work to track down. Hasn't changed in at least 7 years 😄

Exactly how are you declaring your child list?

Edit: Just for fun, I've started doing some forensic digging into history.

So if there's a bug, I'm blaming @jonnybee 😁

rockfordlhotka avatar Dec 13 '19 05:12 rockfordlhotka

Do a simple test to add business validation rules to the class: protected override void AddBusinessRules() { base.AddBusinessRules();
BusinessRules.AddRule(new Csla.Rules.CommonRules.Required(LastNameProperty, "LastName Can't be empty"));

    }

Test results (translated into variable values) private void Button_Click_2(object sender, RoutedEventArgs e) {
person.LastName = ""; var A = person.IsValid;//false var errorText = Csla.Rules.BusinessRules.GetAllBrokenRules(person, true) .First(t => t.BrokenRules.Count > 0) .BrokenRules.First() .Description;//LastName Can't be empty } Problem: person.isvalid should be true if it's normal

angtianqiang avatar Dec 13 '19 05:12 angtianqiang

I don't think there's any bug. Here's code showing how this works.

using System;
using System.Linq;
using Csla;

namespace ChildRules
{
  class Program
  {
    static void Main(string[] args)
    {
      var root = DataPortal.Create<Root>();
      root.ChildList.AddNew();
      Console.WriteLine($"Should be false: {(root.IsSelfValid)}");
      Console.WriteLine($"Should be false: {(root.IsValid)}");
      Console.WriteLine($"Should be false: {(root.ChildList[0].IsValid)}");
      Console.WriteLine($"Should be 2: {BrokenRuleCount(root)}");

      Console.WriteLine();
      root.Name = "abc";
      Console.WriteLine($"Should be true: {(root.IsSelfValid)}");
      Console.WriteLine($"Should be false: {(root.IsValid)}");
      Console.WriteLine($"Should be false: {(root.ChildList[0].IsValid)}");
      Console.WriteLine($"Should be 1: {BrokenRuleCount(root)}");

      Console.WriteLine();
      root.ChildList[0].Name = "abc";
      Console.WriteLine($"Should be true: {(root.IsValid)}");
      Console.WriteLine($"Should be true: {(root.ChildList[0].IsValid)}");
      Console.WriteLine($"Should be 0: {BrokenRuleCount(root)}");

      Console.WriteLine();
      Console.WriteLine("Completed");
      Console.ReadLine();
    }

    private static int BrokenRuleCount(object root)
    {
      var all = Csla.Rules.BusinessRules.GetAllBrokenRules(root);
      return all.Sum(r => r.BrokenRules.Count);
    }
  }

  [Serializable]
  public class Root : BusinessBase<Root>
  {
    public static readonly PropertyInfo<string> NameProperty = RegisterProperty<string>(nameof(Name));
    public string Name
    {
      get => GetProperty(NameProperty);
      set => SetProperty(NameProperty, value);
    }

    public static readonly PropertyInfo<ChildList> ChildListProperty = RegisterProperty<ChildList>(nameof(ChildList));
    public ChildList ChildList
    {
      get => GetProperty(ChildListProperty);
      private set => LoadProperty(ChildListProperty, value);
    }

    protected override void AddBusinessRules()
    {
      BusinessRules.AddRule(new Csla.Rules.CommonRules.Required(NameProperty));
      base.AddBusinessRules();
    }

    [Create]
    private void Create()
    {
      ChildList = DataPortal.CreateChild<ChildList>();
      BusinessRules.CheckRules();
    }
  }

  [Serializable]
  public class ChildList : BusinessListBase<ChildList, Root>
  {
  }
}

rockfordlhotka avatar Dec 13 '19 17:12 rockfordlhotka

I used your code to run and check, and confirmed that there was no bug. After careful analysis, I found that there was a bug in my program. The HasValidationErrors method should have returned null!= the Entity && (!The Entity. The IsValid); And my code says return null == Entity && (!The Entity. IsValid) @rockfordlhotka Thank you very much for the test code, now the program is working

angtianqiang avatar Dec 15 '19 01:12 angtianqiang