Skip to content

Commit 4b87231

Browse files
author
César Cardoso
committed
feature/add-control-erros-and-init-boss
1 parent 9d11147 commit 4b87231

13 files changed

+302
-23
lines changed

Samples/Demo01/C4DValidateComponentsDemo01.dpr

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ uses
1515
C4D.Validate.Components.Consts in '..\..\Src\C4D.Validate.Components.Consts.pas',
1616
C4D.Validate.Components.Language in '..\..\Src\C4D.Validate.Components.Language.pas',
1717
C4D.Validate.Components.Config in '..\..\Src\C4D.Validate.Components.Config.pas',
18-
C4D.Validate.Components.Types in '..\..\Src\C4D.Validate.Components.Types.pas';
18+
C4D.Validate.Components.Types in '..\..\Src\C4D.Validate.Components.Types.pas',
19+
C4D.Validate.Components.Errors in '..\..\Src\C4D.Validate.Components.Errors.pas',
20+
C4D.Validate.Components.ListPair in '..\..\Src\C4D.Validate.Components.ListPair.pas',
21+
C4D.Validate.Components.SetFocus in '..\..\Src\C4D.Validate.Components.SetFocus.pas';
1922

2023
{$R *.res}
2124

Samples/Demo01/C4DValidateComponentsDemo01.dproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@
115115
<DCCReference Include="..\..\Src\C4D.Validate.Components.Language.pas"/>
116116
<DCCReference Include="..\..\Src\C4D.Validate.Components.Config.pas"/>
117117
<DCCReference Include="..\..\Src\C4D.Validate.Components.Types.pas"/>
118+
<DCCReference Include="..\..\Src\C4D.Validate.Components.Errors.pas"/>
119+
<DCCReference Include="..\..\Src\C4D.Validate.Components.ListPair.pas"/>
120+
<DCCReference Include="..\..\Src\C4D.Validate.Components.SetFocus.pas"/>
118121
<BuildConfiguration Include="Release">
119122
<Key>Cfg_2</Key>
120123
<CfgParent>Base</CfgParent>

Samples/Demo01/Src/View/C4D.ValidateComponents.Demo01.View.Main.pas

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ TC4DValidateComponentsDemo01ViewMain = class(TForm)
8484
DBMemo1: TDBMemo;
8585
pnButtons: TPanel;
8686
btnValidar: TButton;
87-
[NotEmpty]
8887
PageControl1: TPageControl;
8988
TabSheet1: TTabSheet;
9089
Edit2: TEdit;
@@ -144,6 +143,7 @@ procedure TC4DValidateComponentsDemo01ViewMain.FormCreate(Sender: TObject);
144143

145144
procedure TC4DValidateComponentsDemo01ViewMain.bntConfigSaveClick(Sender: TObject);
146145
begin
146+
//OPTIONAL CONFIGURATION
147147
TC4DValidateComponents
148148
.Config
149149
.Language(TLanguageDefault(cBoxConfigLanguage.ItemIndex))
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
unit C4D.Validate.Components.Errors;
2+
3+
interface
4+
5+
uses
6+
System.SysUtils,
7+
System.Classes,
8+
C4D.Validate.Components.ListPair;
9+
10+
type
11+
TErros = class
12+
private
13+
FListPairErros: TListPairStr;
14+
constructor Create;
15+
public
16+
class function GetInstance: TErros;
17+
destructor Destroy; override;
18+
procedure Clear;
19+
procedure Add(const AComponentName, AMsg: string);
20+
function GetMessages: string;
21+
function HasErros: Boolean;
22+
function Count: Integer;
23+
function GetNameFirstComponent: string;
24+
end;
25+
26+
implementation
27+
28+
var
29+
Instance: TErros;
30+
31+
class function TErros.GetInstance: TErros;
32+
begin
33+
if(not Assigned(Instance))then
34+
Instance := Self.Create;
35+
Result := Instance;
36+
end;
37+
38+
constructor TErros.Create;
39+
begin
40+
FListPairErros := TListPairStr.Create;
41+
end;
42+
43+
destructor TErros.Destroy;
44+
begin
45+
FListPairErros.Free;
46+
inherited;
47+
end;
48+
49+
procedure TErros.Clear;
50+
begin
51+
FListPairErros.Clear;
52+
end;
53+
54+
procedure TErros.Add(const AComponentName, AMsg: string);
55+
begin
56+
FListPairErros.Add(AComponentName, AMsg);
57+
end;
58+
59+
function TErros.GetMessages: string;
60+
begin
61+
Result := FListPairErros.GetStringsValues;
62+
end;
63+
64+
function TErros.HasErros: Boolean;
65+
begin
66+
Result := FListPairErros.Count > 0;
67+
end;
68+
69+
function TErros.Count: Integer;
70+
begin
71+
Result := FListPairErros.Count;
72+
end;
73+
74+
function TErros.GetNameFirstComponent: string;
75+
begin
76+
Result := '';
77+
78+
if(FListPairErros.Count > 0)then
79+
Result := FListPairErros.Items[0].Key;
80+
end;
81+
82+
initialization
83+
84+
finalization
85+
if(Assigned(Instance))then
86+
FreeAndNil(Instance);
87+
88+
end.

Src/C4D.Validate.Components.Length.pas

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ implementation
2828
C4D.Validate.Components.Helpers,
2929
C4D.Validate.Components.Components,
3030
C4D.Validate.Components.Consts,
31-
C4D.Validate.Components.Language;
31+
C4D.Validate.Components.Language,
32+
C4D.Validate.Components.Errors;
3233

3334
constructor Length.Create(const AMinLength: Integer);
3435
begin
@@ -77,10 +78,7 @@ procedure Length.Validar(const ARttiField: TRttiField; const AObject: TObject);
7778
LText := TC4DValidateComponentsComponents.GetTextFromComponent(LComponent);
7879
LLength := LText.Length;
7980
if(LLength < FMinLength)or((FMaxLength > 0)and(LLength > FMaxLength))then
80-
begin
81-
TC4DValidateComponentsComponents.SetFocu(LComponent);
82-
raise Exception.Create(ARttiField.FormatMsg(Self.GetMsg));
83-
end;
81+
TErros.GetInstance.Add(LComponent.Name, ARttiField.FormatMsg(Self.GetMsg));
8482
end;
8583

8684
end.
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
unit C4D.Validate.Components.ListPair;
2+
3+
interface
4+
5+
uses
6+
System.Types,
7+
System.SysUtils,
8+
System.Classes,
9+
System.Generics.Defaults,
10+
System.Generics.Collections;
11+
12+
type
13+
TListPair<TKey, TValue> = class(TList<TPair<TKey, TValue>>)
14+
protected
15+
FKeyComparer: IComparer<TKey>;
16+
FValueComparer: IComparer<TValue>;
17+
function GetValue(Key: TKey): TValue;
18+
procedure SetValue(Key: TKey; const Value: TValue);
19+
function ComparePair(const Left, Right: TPair<TKey, TValue>): Integer;
20+
public
21+
constructor Create; overload;
22+
procedure Add(const AKey: TKey; const aValue: TValue); overload;
23+
function IndexOfKey(const AKey: TKey): Integer;
24+
function ContainsKey(const AKey: TKey): Boolean; inline;
25+
property Values[Key: TKey]: TValue read GetValue write SetValue;
26+
end;
27+
28+
TListPairStr = class(TListPair<string, string>);
29+
30+
TListPairStrHelper = class helper for TListPairStr
31+
procedure FillStringsWithValues(const AStrings: TStrings);
32+
function GetStringsValues: string;
33+
end;
34+
35+
implementation
36+
37+
constructor TListPair<TKey, TValue>.Create;
38+
begin
39+
if(FKeyComparer = nil)then
40+
FKeyComparer := TComparer<TKey>.Default;
41+
if(FValueComparer = nil)then
42+
FValueComparer := TComparer<TValue>.Default;
43+
inherited Create(TDelegatedComparer <TPair<TKey, TValue>>.Create(ComparePair));
44+
end;
45+
46+
function TListPair<TKey, TValue>.ComparePair(const Left, Right: TPair<TKey, TValue>): Integer;
47+
begin
48+
Result := FKeyComparer.Compare(Left.Key, Right.Key);
49+
if(Result = 0)then
50+
Result := FValueComparer.Compare(Left.Value, Right.Value);
51+
end;
52+
53+
function TListPair<TKey, TValue>.IndexOfKey(const AKey: TKey): Integer;
54+
var
55+
i: Integer;
56+
begin
57+
Result := -1;
58+
for i := 0 to Count - 1 do
59+
begin
60+
if(FKeyComparer.Compare(Items[i].Key, AKey) = 0)then
61+
begin
62+
Result := i;
63+
break;
64+
end;
65+
end;
66+
end;
67+
68+
function TListPair<TKey, TValue>.ContainsKey(const AKey: TKey): Boolean;
69+
begin
70+
Result := IndexOfKey(AKey) >= 0;
71+
end;
72+
73+
function TListPair<TKey, TValue>.GetValue(Key: TKey): TValue;
74+
var
75+
i: Integer;
76+
begin
77+
i := IndexOfKey(Key);
78+
if(i >= 0)then
79+
Result := Items[i].Value
80+
else
81+
Result := default(TValue);
82+
end;
83+
84+
procedure TListPair<TKey, TValue>.Add(const AKey: TKey; const aValue: TValue);
85+
begin
86+
SetValue(AKey, aValue);
87+
end;
88+
89+
procedure TListPair<TKey, TValue>.SetValue(Key: TKey; const Value: TValue);
90+
begin
91+
inherited Add(TPair<TKey, TValue>.Create(Key, Value));
92+
end;
93+
94+
{ TListPairStrHelper }
95+
procedure TListPairStrHelper.FillStringsWithValues(const AStrings: TStrings);
96+
var
97+
LPairErro: TPair<string, string>;
98+
begin
99+
if(Self.Count > 0)then
100+
for LPairErro in Self do
101+
AStrings.Add('* ' + LPairErro.Value);
102+
end;
103+
104+
function TListPairStrHelper.GetStringsValues: string;
105+
var
106+
LPairErro: TPair<string, string>;
107+
begin
108+
Result := '';
109+
if(Self.Count > 0)then
110+
for LPairErro in Self do
111+
Result := Result + '* ' + LPairErro.Value + sLineBreak;
112+
end;
113+
114+
end.

Src/C4D.Validate.Components.MinMaxDate.pas

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ implementation
2727
C4D.Validate.Components.Helpers,
2828
C4D.Validate.Components.Components,
2929
C4D.Validate.Components.Consts,
30-
C4D.Validate.Components.Language;
30+
C4D.Validate.Components.Language,
31+
C4D.Validate.Components.Errors;
3132

3233
constructor MinMaxDate.Create(const AMinDate: string);
3334
begin
@@ -71,10 +72,7 @@ procedure MinMaxDate.Validar(const ARttiField: TRttiField; const AObject: TObjec
7172
LText := TC4DValidateComponentsComponents.GetTextFromComponent(LComponent);
7273
LDate := StrToDateDef(LText, 0);
7374
if(LDate < FMinDate)or((FMaxDate > 0)and(LDate > FMaxDate))then
74-
begin
75-
TC4DValidateComponentsComponents.SetFocu(LComponent);
76-
raise Exception.Create(ARttiField.FormatMsg(Self.GetMsg));
77-
end;
75+
TErros.GetInstance.Add(LComponent.Name, ARttiField.FormatMsg(Self.GetMsg));
7876
end;
7977

8078
end.

Src/C4D.Validate.Components.MinMaxValue.pas

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ implementation
2828
C4D.Validate.Components.Helpers,
2929
C4D.Validate.Components.Components,
3030
C4D.Validate.Components.Consts,
31-
C4D.Validate.Components.Language;
31+
C4D.Validate.Components.Language,
32+
C4D.Validate.Components.Errors;
3233

3334
constructor MinMaxValue.Create(const AMinValue: Integer);
3435
begin
@@ -77,10 +78,7 @@ procedure MinMaxValue.Validar(const ARttiField: TRttiField; const AObject: TObje
7778
LText := TC4DValidateComponentsComponents.GetTextFromComponent(LComponent);
7879
LValue := StrToFloatDef(LText, 0);
7980
if(LValue < FMinValue)or((FMaxValue > 0)and(LValue > FMaxValue))then
80-
begin
81-
TC4DValidateComponentsComponents.SetFocu(LComponent);
82-
raise Exception.Create(ARttiField.FormatMsg(Self.GetMsg));
83-
end;
81+
TErros.GetInstance.Add(LComponent.Name, ARttiField.FormatMsg(Self.GetMsg));
8482
end;
8583

8684
end.

Src/C4D.Validate.Components.NotEmpty.pas

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ implementation
2222
uses
2323
C4D.Validate.Components.Helpers,
2424
C4D.Validate.Components.Components,
25-
C4D.Validate.Components.Language;
25+
C4D.Validate.Components.Language,
26+
C4D.Validate.Components.Errors;
2627

2728
constructor NotEmpty.Create;
2829
begin
@@ -43,10 +44,7 @@ procedure NotEmpty.Validar(const ARttiField: TRttiField; const AObject: TObject)
4344
LText := TC4DValidateComponentsComponents.GetTextFromComponent(LComponent);
4445

4546
if(LText.Trim.IsEmpty)then
46-
begin
47-
TC4DValidateComponentsComponents.SetFocu(LComponent);
48-
raise Exception.Create(ARttiField.FormatMsg(FMsg));
49-
end;
47+
TErros.GetInstance.Add(LComponent.Name, ARttiField.FormatMsg(FMsg));
5048
end;
5149

5250
end.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
unit C4D.Validate.Components.SetFocus;
2+
3+
interface
4+
5+
uses
6+
System.SysUtils,
7+
System.RTTI,
8+
System.Classes,
9+
Vcl.Forms,
10+
Vcl.Controls,
11+
Vcl.ComCtrls;
12+
13+
type
14+
TRttiSetFocus = class
15+
private
16+
class procedure SetFocusComponent(const AWinControl: TWinControl);
17+
public
18+
class procedure SetFocusComponentName(const AForm: TForm; const AComponentName: string);
19+
end;
20+
21+
implementation
22+
23+
class procedure TRttiSetFocus.SetFocusComponentName(const AForm: TForm; const AComponentName: string);
24+
var
25+
LComponent: TComponent;
26+
begin
27+
LComponent := AForm.FindComponent(AComponentName);
28+
if(LComponent is TWinControl)then
29+
SetFocusComponent(TWinControl(LComponent));
30+
end;
31+
32+
class procedure TRttiSetFocus.SetFocusComponent(const AWinControl: TWinControl);
33+
var
34+
LParent: TComponent;
35+
begin
36+
LParent := TWinControl(AWinControl).Parent;
37+
while(LParent.ClassParent <> TForm)do
38+
begin
39+
if(LParent is TTabSheet)then
40+
if(not TTabSheet(LParent).Showing)then
41+
TTabSheet(LParent).Show;
42+
43+
LParent := TWinControl(LParent).Parent;
44+
end;
45+
46+
try
47+
if(TWinControl(AWinControl).CanFocus)then
48+
TWinControl(AWinControl).SetFocus;
49+
except
50+
end;
51+
end;
52+
53+
end.

0 commit comments

Comments
 (0)