我需要TField
根据字段本身的 previos 值验证 a 的新值。
例如:字段的值只能更改为更大的值
procedure TForm1.FldOnValidate(AField : TField);
begin
if(???) then
raise Exception.Create('The new value is not bigger than the previous one');
end;
procedure TForm1.FormCreate(Sender: TObject);
var
Dst : TClientDataSet;
Fld : TIntegerField;
begin
//dataset
Dst := TClientDataSet.Create(Self);
Dst.FieldDefs.Add('TEST', ftInteger, 0);
Dst.CreateDataSet();
Dst.Active := True;
Fld := Dst.Fields[0] as TIntegerField;
Dst.Append();
Fld.AsInteger := 5;
Dst.Post();
Fld.OnValidate := FldOnValidate;
//this should be ok (from 5 to 6)
Dst.Edit;
Fld.AsInteger := 6;
Dst.Post;
//this should not pass the validation (from 6 to 5)
Dst.Edit;
Fld.AsInteger := 5;
end;
我试图检查OldValue
,和属性NewValue
,但我总是得到新值:AsVariant
Value
procedure TForm1.FldOnValidate(AField : TField);
begin
ShowMessage(
'OldValue = ' + VarToStr(AField.OldValue) + sLineBreak +
'NewValue = ' + VarToStr(AField.NewValue) + sLineBreak +
'AsVariant = ' + VarToStr(AField.AsVariant) + sLineBreak +
'Value = ' + VarToStr(AField.Value)
);
end;
希望有人能启发我