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 中测试了一下, 没有问题.