5

I'm trying to write an OnValidate event on a field on a TClientDataSet, something along the lines of

procedure TForm8.ClientDataSet1MyFieldValidate(Sender: TField);
begin
  if Sender.AsFloat > 30 then
     raise Exception.Create('Too Much!!!');
end;

But Sender.AsFloat is always 0 - how do I do field level validation (I realize in this instance I could use constraints or set Min/Max values)

Alister
  • 6,527
  • 4
  • 46
  • 70
  • 1
    I can't see any sensible reason, why would the field (that `Sender`) return a wrong value. Is that really a floating point value field (like `TFloatField` or similar) ? Could you post the full field's definition (from the form's source, when you press ALT + F12 in form designer) ? – TLama Nov 01 '12 at 03:33
  • @TLama There is not much to it, put on a TClientDataSet, TDBGrid, TDataSource and hook them up. Add a TFloatField to the DBGrid, then CreateDataSet. Add the onValidate event to the field, Run and add a value to the grid. – Alister Nov 01 '12 at 03:58
  • 1
    works perfectly for me. Delphi XE2. – Jason Nov 01 '12 at 04:22
  • For me as well (Delphi 2009 if that matters). Except mixing data types or another field from a dataset row using the same `OnValidate` event as common doesn't anything else come to my mind. – TLama Nov 01 '12 at 05:00
  • 1
    @Alister, I did exactly as written in your comment (except the TFloatField is added to the CDS not the Grid), and it works perfectly as expected in XE. (so we have now 2009, 2010, XE and XE2 covered)... You'd have to paste the dfm text and your code to give a clue at what might be different. – Francesca Nov 01 '12 at 18:02
  • This works fine in D2007 Also, can someone else confirm this is an issue with XE3? – Alister Nov 02 '12 at 00:40
  • 1
    @Alister, it repeats here in XE3 :( – jachguate Nov 02 '12 at 06:06

3 Answers3

3

This is a bug that has been introduced in Delphi XE3, here is the QC report, and a quick movie I've made to illustrate the problem clearly. Hopefully this will be fixed in the next update. There is a hot fix in one of the comments on the QC page if you need this fixed immediately.

Alister
  • 6,527
  • 4
  • 46
  • 70
1

this works fine on D2010, where's the difference ...

procedure TForm3.FloatValidate(Sender: TField);
begin
  if sender.AsFloat > 30 then Showmessage('No');

end;

procedure TForm3.Button1Click(Sender: TObject);
begin
   With Clientdataset1 do
    begin
      FieldDefs.add('ID',ftInteger,0);
      FieldDefs.add('Floatfield',ftFloat,0);
      Createdataset;
      Fields[1].OnValidate := FloatValidate;
    end;

end;
bummi
  • 27,123
  • 14
  • 62
  • 101
  • Well, so now we know that the `OnValidate` event is fired as expected in Delphi 2009, 2010 and XE2... – TLama Nov 01 '12 at 10:43
  • Yup, this is pretty much what I've got. It seems to be an issue with XE3, I guess I should file a bug report. – Alister Nov 02 '12 at 00:42
  • have a look http://www.delphipraxis.net/170884-access-violations-nach-update-auf-xe3.html, there seem to be more problems with Tclientdataset on XE3 – bummi Nov 02 '12 at 01:01
0

You might want to check if Sender.NewValue contains the value you are after. If the update cache on the client dataset is active you can use the OldValue, Value and NewValue of the fields.

Larsdk
  • 705
  • 6
  • 10