Skip to content

Commit 84cde61

Browse files
author
César Cardoso
committed
bk/2023-11-23-0946
1 parent 39654f2 commit 84cde61

2 files changed

Lines changed: 53 additions & 32 deletions

File tree

Samples/Demo01/Src/View/C4DValidateComponentsDemo01.View.Main.pas

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ interface
1616
Vcl.ExtCtrls,
1717
Vcl.ComCtrls,
1818
System.RTTI,
19-
C4D.Validate.Components.NotEmpty,
20-
C4D.Validate.Components.Length,
21-
C4D.Validate.Components.MinMaxValue,
22-
C4D.Validate.Components.FieldDisplay;
19+
C4D.Validate.Components;
2320

2421
type
2522
TC4DValidateComponentsDemo01ViewMain = class(TForm)
@@ -54,6 +51,7 @@ TC4DValidateComponentsDemo01ViewMain = class(TForm)
5451

5552
[NotEmpty]
5653
Memo1: TMemo;
54+
5755
ckCheck: TCheckBox;
5856
pnBotoes: TPanel;
5957
btnValidar: TButton;
@@ -105,35 +103,9 @@ procedure TC4DValidateComponentsDemo01ViewMain.btnClearAllFieldsClick(Sender: TO
105103
end;
106104

107105
procedure TC4DValidateComponentsDemo01ViewMain.btnValidarClick(Sender: TObject);
108-
var
109-
LRttiContext: TRttiContext;
110-
LRttiType: TRttiType;
111-
LRttiField: TRttiField;
112-
LCustomAttribute: TCustomAttribute;
113106
begin
114-
LRttiContext := TRttiContext.Create;
115-
try
116-
LRttiType := LRttiContext.GetType(TC4DValidateComponentsDemo01ViewMain);
117-
118-
for LRttiField in LRttiType.GetFields do
119-
begin
120-
for LCustomAttribute in LRttiField.GetAttributes do
121-
begin
122-
if(LCustomAttribute is NotEmpty)then
123-
NotEmpty(LCustomAttribute).Validar(LRttiField, Self);
124-
125-
if(LCustomAttribute is Length)then
126-
Length(LCustomAttribute).Validar(LRttiField, Self);
127-
128-
if(LCustomAttribute is MinMaxValue)then
129-
MinMaxValue(LCustomAttribute).Validar(LRttiField, Self);
130-
end;
131-
end;
132-
finally
133-
LRttiContext.Free;
134-
end;
135-
136-
ShowMeSsage('SUCESSO')
107+
TC4DValidateComponents.Validate(TC4DValidateComponentsDemo01ViewMain, Self);
108+
ShowMeSsage('SUCESSO');
137109
end;
138110

139111
end.

Src/C4D.Validate.Components.pas

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,61 @@
22

33
interface
44

5+
uses
6+
System.SysUtils,
7+
System.Classes,
8+
System.RTTI,
9+
Vcl.Forms,
10+
C4D.Validate.Components.NotEmpty,
11+
C4D.Validate.Components.Length,
12+
C4D.Validate.Components.MinMaxValue,
13+
C4D.Validate.Components.FieldDisplay;
14+
515
type
16+
NotEmpty = C4D.Validate.Components.NotEmpty.NotEmpty;
17+
Length = C4D.Validate.Components.Length.Length;
18+
MinMaxValue = C4D.Validate.Components.MinMaxValue.MinMaxValue;
19+
FieldDisplay = C4D.Validate.Components.FieldDisplay.FieldDisplay;
20+
621
TC4DValidateComponents = class
722
private
823
public
24+
class procedure Validate(AInstanceClass: TComponentClass; const AForm: TForm);
925
end;
1026

1127
implementation
1228

29+
class procedure TC4DValidateComponents.Validate(AInstanceClass: TComponentClass; const AForm: TForm);
30+
var
31+
LRttiContext: TRttiContext;
32+
LRttiType: TRttiType;
33+
LRttiField: TRttiField;
34+
LCustomAttribute: TCustomAttribute;
35+
begin
36+
LRttiContext := TRttiContext.Create;
37+
try
38+
LRttiType := LRttiContext.GetType(AInstanceClass);
39+
40+
for LRttiField in LRttiType.GetFields do
41+
begin
42+
if(LRttiField.Parent <> LRttiType)then
43+
Continue;
44+
45+
for LCustomAttribute in LRttiField.GetAttributes do
46+
begin
47+
if(LCustomAttribute is NotEmpty)then
48+
NotEmpty(LCustomAttribute).Validar(LRttiField, AForm);
49+
50+
if(LCustomAttribute is Length)then
51+
Length(LCustomAttribute).Validar(LRttiField, AForm);
52+
53+
if(LCustomAttribute is MinMaxValue)then
54+
MinMaxValue(LCustomAttribute).Validar(LRttiField, AForm);
55+
end;
56+
end;
57+
finally
58+
LRttiContext.Free;
59+
end;
60+
end;
61+
1362
end.

0 commit comments

Comments
 (0)