Validating objects by their constraints

You may have noticed by now that by default Bold will allow you to save objects with invalid constraints. Here is a solution.

Override MayUpdate in your base class (BusinessClassesRoot in this example) and include the following code....



function TBusinessClassesRoot.MayUpdate: Boolean;

var

  I: Integer;

  Errors: TStringList;

  IE: TBoldIndirectElement;

  List: TBoldList;

begin

  IE := TBoldIndirectElement.Create;

  Errors := TStringList.Create;

  try

    try

      EvaluateExpression('constraints->select(c| not c)', IE);

      List := IE.Value as TBoldList;

      for I := 0 to List.Count - 1 do

        Errors.Add(List[0].AsString);


      Result := (Errors.Count = 0);

      if not Result then

      begin

        BoldClearLastFailure;

        SetBoldLastFailureReason(TBoldFailureReason.Create(Errors.Text, Self));

      end;

    except

      Result := False;

      raise;

    end;

  finally

    IE.Free;

    Errors.Free;

  end;

end;



If an attempt is made to save an object with broken constraints an error will occur.

 

Share this article!

Follow us!

Find more helpful articles: