Recent

Author Topic: OnValidate problem  (Read 9422 times)

99Percent

  • Full Member
  • ***
  • Posts: 154
OnValidate problem
« on: September 26, 2010, 05:19:58 am »
In Delphi you use TField.OnValidate to make sure no bad data is input in a field. If there is any objection  to this data you raise an exception. You can even just call abort so a user that already knows that the data is bad simply corrects it without an annoying message.

But in Lazarus raising an exception either just notifies the user something went wrong and continues after where the OnValidate was called, or the user can click Cancel to completely obliterate the program. This differs radically from the Delphi implementation.

So the question is how can I write an effective TField.OnValidate event in Lazarus?

As a reference read this mailing list topic: http://www.mail-archive.com/lazarus@lazarus.freepascal.org/msg07863.html

99Percent

  • Full Member
  • ***
  • Posts: 154
Re: OnValidate problem
« Reply #1 on: September 27, 2010, 06:14:16 am »
I notice that LCL doesn't actually come with any TField objects that have OnValidate property. I was using Unidac's TVirtualTable which does actually work in Lazarus, except precisely the OnValidate property.

I tried instead of creating an exception, to show a messagedlg, followed by focus to the grid of the VirtualTable and then Grid.SelectedField to the field of the OnValidate event. And it mostly works except for when you move between columns of the grid. For some reason and I don't know if it's Lazarus's fault (I hope not) or Unidac's fault, but the OnValidate is called before the column is actually selected internally in the grid but after the event of the move cursor to another column of the grid is already processed. So when in OnValidate procedure you try to Assign the SelectedField, it is ignored because the DBGrid object thinks its still on the OnValidate field.

In other words its a big mess to say the least. So I am going to try to forget about Unidac's VirtualTable component and work with TMemDataset instead.

I hope this helps someone in the future.

Lacak2

  • Guest
Re: OnValidate problem
« Reply #2 on: September 29, 2010, 02:27:36 pm »
Interesting, when I look at TField.Validate method (which calls OnValidate), then implementation in FPC and Delphi is equal.

So difference may be in general exception handling between FPC and Delphi

99Percent

  • Full Member
  • ***
  • Posts: 154
Re: OnValidate problem
« Reply #3 on: September 29, 2010, 06:16:14 pm »
I've been investigating this OnValidate problem extensively. I believe (although I am not 100% sure) the TField.OnValidate event is never executed.

And looking at the code (line 886 in fields.inc) that even if it does get executed, it will not behave the same as in Delphi, for it only calls the OnValidate code, and doesn't cancel any future events to make sure editing continues on the DB aware component handling this field.

99Percent

  • Full Member
  • ***
  • Posts: 154
Re: OnValidate problem
« Reply #4 on: September 29, 2010, 08:48:46 pm »
I reported this as a bug in the bugtracker:

http://bugs.freepascal.org/view.php?id=17510

Lacak2

  • Guest
Re: OnValidate problem
« Reply #5 on: September 30, 2010, 07:43:57 am »
Yes, it seems, that you are right. Validate method is never called ;-)