hcOPF Dialog Validation

Reminder to self, when presenting a dialog for editing an hcOPF object a generic OK button click or action Execute handler is:

procedure TfrmObjectDialog.btOKClick(Sender: TObject);
var
  ValidationErrorList: ThcValidationErrorList;
begin
  //switch focus to another TWinControl to ensure the current focused editor
  //updates it's Subject
  SelectNext(ActiveControl as TWinControl,True,True);

  ValidationErrorList := ThcValidationErrorList.Create();
  try
    if hcUIObjectBinder.BoundObject.IsValid(ValidationErrorList) then
    begin
      hcUIObjectBinder.BoundObject.Write(osRDBMS,False);
      hcUIObjectBinder.BoundObject := nil;
      ModalResult := mrOk;
      Close;
    end
    else
    begin
      MessageDlg(Format('Please Correct the Following Error(s)'#13#10#13#10'%s',
        [ValidationErrorList.Text]),mtWarning,[mbOk],0);

        //focus editor for first invalid attribute
        if (ValidationErrorList.Count > 0) and
         (assigned(ValidationErrorList.Items[0].Attribute)) then
        hcUIObjectBinder.FocusControlForAttribute(
           ValidationErrorList.Items[0].Attribute
        );

     ModalResult := mrNone;
    end;
  finally
    ValidationErrorList.Free;
  end;
end;

And for the Cancel button:

procedure TfrmObjectDialog.btCancelClick(Sender: TObject);
begin
  hcUIObjectBinder.BoundObject.UndoChanges;
  hcUIObjectBinder.BoundObject := nil;
  ModalResult := mrCancel;
  Close;
end;

or if you are using the VCL you can Inherit or Copy the hcDialog object found in the \Source\UI\VCL folder. I would suggest adding it to the Object Repository to make it easier to do so. There are comments in the unit suggesting how to use either design-time or run-time bindings.

Leave a Reply