Required、Precision、MaxValue、MinValue:
begin
{ Required: 必填字段 }
with TIntegerFIEld.Create(Self) do begin
FIEldName := 'ID';
Required := True;
DataSet := ClIEntDataSet1;
end;
{ Precision: 浮點數精度}
with TFloatFIEld.Create(Self) do begin
FIEldName := 'Float';
Precision := 3; { 譬如: 輸入 1.2345 只會保留 1.23 }
DataSet := ClIEntDataSet1;
end;
{ MaxValue、MinValue }
with TIntegerFIEld.Create(Self) do begin
FIEldName := 'Integer';
MinValue := 1;
MaxValue := 99;
DataSet := ClIEntDataSet1;
end;
ClIEntDataSet1.CreateDataSet;
end;
可在字段的 CustomConstraint 屬性中按 SQL 語法指定約束規則;
並用字段的 ConstraintErrorMessage 屬性指定違反規則後的錯誤提示.
procedure TForm1.FormCreate(Sender: TObject);
begin
with TIntegerFIEld.Create(Self) do begin
FIEldName := 'ID';
CustomConstraint := 'x>0 and x<100'; { 其中的 x 是隨意的, 表示當前字段值 }
ConstraintErrorMessage := 'Err1';
DataSet := ClIEntDataSet1;
end;
with TStringFIEld.Create(Self) do begin
FIEldName := 'Name';
Size := 11;
CustomConstraint := 'x Like ''張%'''; { 假如只要姓張的 }
ConstraintErrorMessage := 'Err2';
DataSet := ClIEntDataSet1;
end;
with TStringFIEld.Create(Self) do begin
FIEldName := 'Sex';
Size := 2; { 如果使用 TWideStringFIEld 這裡應該是 1 }
CustomConstraint := 'x in(''男'', ''女'')'; { 只能輸入: 男或女 }
ConstraintErrorMessage := 'Err3';
DataSet := ClIEntDataSet1;
end;
with TStringFIEld.Create(Self) do begin
FIEldName := 'Email';
Size := 21;
CustomConstraint := 'Lower(x) Like ''%@gmail.com'''; { 假如只能是 Google 信箱}
ConstraintErrorMessage := 'Err4';
DataSet := ClIEntDataSet1;
end;
ClIEntDataSet1.CreateDataSet;
ClIEntDataSet1.AppendRecord([1, '張三', '男', '[email protected]']);
ClIEntDataSet1.AppendRecord([2, '張四', '女', '[email protected]']);
end;
{ 禁用限制 }
procedure TForm1.Button1Click(Sender: TObject);
begin
ClIEntDataSet1.DisableConstraints;
end;
{ 啟用限制 }
procedure TForm1.Button2Click(Sender: TObject);
begin
ClIEntDataSet1.EnableConstraints;
end;
使用數據集的 Constraints 屬性重做上面的例子:
procedure TForm1.FormCreate(Sender: TObject);
begin
with ClientDataSet1.FIEldDefs do begin
Add('ID', ftInteger);
Add('Name', ftString, 11);
Add('Sex', ftString, 2);
Add('Email', ftString, 21);
end;
with ClIEntDataSet1.Constraints.Add do begin
CustomConstraint := 'ID>0 and ID<100'; { 其中的 ID 是字段名 }
ErrorMessage := 'Err1';
end;
with ClIEntDataSet1.Constraints.Add do begin
CustomConstraint := 'Name Like ''張%''';
ErrorMessage := 'Err2';
end;
with ClIEntDataSet1.Constraints.Add do begin
CustomConstraint := 'Sex in(''男'', ''女'')';
ErrorMessage := 'Err3';
end;
with ClIEntDataSet1.Constraints.Add do begin
CustomConstraint := 'Lower(Email) Like ''%@gmail.com''';
ErrorMessage := 'Err4';
end;
ClIEntDataSet1.CreateDataSet;
ClIEntDataSet1.AppendRecord([1, '張三', '男', '[email protected]']);
ClIEntDataSet1.AppendRecord([2, '張四', '女', '[email protected]']);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ClIEntDataSet1.DisableConstraints;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
ClIEntDataSet1.EnableConstraints;
end;
數據集的 Constraints 和字段的 CustomConstraint 也都可以在設計時完成.
不過其中的 ConstraintErrorMessage 和 ErrorMessage 在當前版本(14.0.3593.25826)中有 bug;
我在 Delphi 2007 中測試了一下, 沒有問題.