避免调用重复进行验证

时间:2019-02-18 17:02:58

标签: delphi delphi-10.1-berlin delphi-10.2-tokyo

我有点卡住了。我的目的是始终在从内置代码或用户界面更改值时检查并验证我的货币字段。是否有任何验证模式或示例?我想两次调用验证程序。

任何帮助或建议,将不胜感激。

这是我的例子:

type
TMyForm = class(TForm)   
  ceMyCurrencyEdit: TcxCurrencyEdit;
  procedure FormShow(Sender: TObject);
private
{ Private declarations }
  procedure SetupMyForm;
public
{ Public declarations }
  procedure ValidateMyCurrencyValue;
  function IsValidCurrency: Boolean;
end; 

procedure TMyForm.ValidateMyCurrencyField;
begin
  if not IsValidCurrency then
  begin
    WarningDlg(
       Format(
           'InValid value in field: [%s..%s]',
           [Formatfloat(ceMyCurrencyEditDisplayFormat, ceMyCurrencyEdit.MinValue),
           Formatfloat(ceMyCurrencyEdit.DisplayFormat, ceMyCurrencyEdit.MaxValue)]
        )
    );
    ceMyCurrencyEdit.Value := ceMyCurrencyEdit.MinValue; 
  end;   
end;

function TMyForm.IsValidCurrency: Boolean;
begin
  Result := (ceMyCurrencyEdit.Value >= ceMyCurrencyEdit.MinValue) and (ceMyCurrencyEdit.Value <= ceMyCurrencyEdit.MaxValue);  
end;

procedure TMyForm.SetupMyForm;
begin
  //MaxValue is 100
  ceMyCurrencyEdit.Value := 102;
  //At this point I need to call ValidateMyCurrencyField to get warning msg and refuse its value
  ValidateMyCurrencyField;
end;

procedure TMyForm.FormShow(Sender: TObject);
begin
  SetupMyForm;
end;

procedure TMyForm.ceMyCurrencyEditPropertiesEditValueChanged(Sender: TObject);
begin
  ValidateMyCurrencyField;
end;

我想要的是...

感谢您的回答!

1 个答案:

答案 0 :(得分:1)

我不确定为什么您似乎对@Nil的建议有点不屑一顾,但是下面 是一个示例项目,显示了如何使用cxCurrencyEdit的验证工具 并使用中介层TcxCurrencyEdit类记录验证结果 在其ValidationState属性中进行处理。

很明显,您可以使用ValidationState属性来避免重复 您的验证过程,或者如果状态表明它是有效的,则可以进行验证。 但是,从性能角度来看,我非常怀疑您是否会自救 任何东西。

很明显,插入器类的要点是记录验证状态 控件,这样您就可以避免再次进行验证。是否将其与cxCurrencyEdit1PropertiesValidate事件结合使用完全取决于您。

我让您将其与您现有的代码结合起来。

顺便说一句,TEdit控件就在那里,可以将焦点从cxCurrencyEdit转移到触发其cxCurrencyEdit1PropertiesValidate事件。

更新。您在评论中询问有关验证代码中设置的值的问题。用 一个中介层类,很容易为Value属性添加Getter和Setter 并在设置器中进行验证-参见下面的“ SetValue”,

  type
    TCurrencyValidation = (cvNotDone, cvOK, cvInvalid);

    TcxCurrencyEdit = class(cxCurrencyEdit.TcxCurrencyEdit)
    private
      FValidationState : TCurrencyValidation;
      function GetValue: Double;
      procedure SetValue(const AValue: Double);
    protected
      property ValidationState : TCurrencyValidation read FValidationState write FValidationState;
    published
      property Value : Double read GetValue write SetValue;
    end;

    TForm1 = class(TForm)
      cxCurrencyEdit1: TcxCurrencyEdit;
      Edit1: TEdit;
      procedure FormCreate(Sender: TObject);
      procedure cxCurrencyEdit1PropertiesChange(Sender: TObject);
      procedure cxCurrencyEdit1PropertiesValidate(Sender: TObject;
        var DisplayValue: Variant; var ErrorText: TCaption;
        var Error: Boolean);
    end;

  procedure TForm1.FormCreate(Sender: TObject);
  begin
    cxCurrencyEdit1.Properties.MinValue := 5;
    cxCurrencyEdit1.Properties.MaxValue := 10;
    cxCurrencyEdit1.Value := 8;
  end;

  procedure TForm1.cxCurrencyEdit1PropertiesChange(Sender: TObject);
  begin
    TcxCurrencyEdit(Sender).ValidationState := cvNotDone;
  end;

  procedure TForm1.cxCurrencyEdit1PropertiesValidate(Sender: TObject;
    var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
  var
    Min,
    Max : Double;
  begin
    Min := TcxCurrencyEdit(Sender).Properties.MinValue;
    Max := TcxCurrencyEdit(Sender).Properties.MaxValue;
    if (DisplayValue >= Min) and (DisplayValue <= Max) then
      TcxCurrencyEdit(Sender).ValidationState := cvOK
    else
      TcxCurrencyEdit(Sender).ValidationState := cvInvalid;
    Error := not (TcxCurrencyEdit(Sender).ValidationState = cvOK);
    if Error then
      ErrorText := 'InvalidValue';
  end;

  function TcxCurrencyEdit.GetValue: Double;
  begin
    Result := inherited Value;
  end;

  procedure TcxCurrencyEdit.SetValue(const AValue: Double);
  begin
    //  insert code to validate AValue here
    inherited Value := AValue;
  end;