zoukankan      html  css  js  c++  java
  • TClientDataSet[27]: 字段值的约束(或叫输入限制)


    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, '张三', '男', '123@gmail.com']);
      ClientDataSet1.AppendRecord([2, '张四', '女', 'ABC@GMAIL.COM']);
    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, '张三', '男', '123@gmail.com']);
      ClientDataSet1.AppendRecord([2, '张四', '女', 'ABC@GMAIL.COM']);
    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 中测试了一下, 没有问题.

  • 相关阅读:
    jsp——学习篇:简单使用CSS
    MongoDB分片式服务器集群配置
    PHP上传文件
    【转】【thinkphp3.x】thinkphp3.x中display方法及show方法的使用
    (转)Mysql用户与权限管理
    MonkeyRunner 之如何获取APP的Package Name和Activity Name
    一个典型的PHP分页实例代码分享
    HTML meta charset 定义网页编码信息
    【转】PHP连接MySQL数据库
    一个漂亮的php验证码类(分享)
  • 原文地址:https://www.cnblogs.com/del/p/1666783.html
Copyright © 2011-2022 走看看