Recent

Author Topic: Database field validation using onSetText  (Read 1326 times)

joaoclobocar

  • Newbie
  • Posts: 2
Database field validation using onSetText
« on: May 13, 2021, 08:52:29 pm »
I am moving to Lazarus, and currently facing a problem validating database fields.
I have created OnSetText event for field validation and if not valid I raise an exception.
Code: Pascal  [Select][+][-]
  1. procedure TModelLancamentoFinanceiro.SetTextValorDocumento(Sender: TField; const aText: string);
  2. var
  3.   Valor : Currency;
  4. begin
  5.   Valor := StrToCurr(aText);
  6.   if Valor <> 1 then
  7.   begin
  8.     raise Exception.Create(Format('o conteúdo %s não é válido para o campo %s', [aText, 'Valor']));
  9.   end
  10.   else
  11.     Sender.AsCurrency := Valor;
  12. end;
  13.  
I have entered "2" in the DBEdit field.
If I type Enter key while in the DBEdit field, exception is raised and the current field value is shown back in the DBEdit. That is great.
If I type Tab or change to the other field using mouse click, exception is raised, cursor move to the field, and the number shown in the DBEdit field is the invalid value "2".
How to keep cursor in DBEdit field while its content is invalid?
I have searched for a solution, tried some hacks, but having no success.

 

TinyPortal © 2005-2018