Skip to content

Commit 9bc58a1

Browse files
author
César Cardoso
committed
feature/2023-11-22-1737
1 parent e627a19 commit 9bc58a1

4 files changed

Lines changed: 85 additions & 31 deletions

File tree

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

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ object C4DValidateComponentsDemo01ViewMain: TC4DValidateComponentsDemo01ViewMain
22
Left = 0
33
Top = 0
44
Caption = 'C4D-validate-components - Demo01'
5-
ClientHeight = 533
5+
ClientHeight = 588
66
ClientWidth = 662
77
Color = clBtnFace
88
Font.Charset = DEFAULT_CHARSET
@@ -17,17 +17,19 @@ object C4DValidateComponentsDemo01ViewMain: TC4DValidateComponentsDemo01ViewMain
1717
Left = 0
1818
Top = 0
1919
Width = 662
20-
Height = 533
20+
Height = 588
2121
Align = alClient
2222
BevelOuter = bvNone
2323
TabOrder = 0
24+
ExplicitHeight = 533
2425
object pnTopo: TPanel
2526
Left = 0
2627
Top = 0
2728
Width = 662
28-
Height = 488
29+
Height = 543
2930
Align = alClient
3031
TabOrder = 0
32+
ExplicitHeight = 488
3133
object Label1: TLabel
3234
Left = 24
3335
Top = 11
@@ -115,7 +117,7 @@ object C4DValidateComponentsDemo01ViewMain: TC4DValidateComponentsDemo01ViewMain
115117
end
116118
object Memo1: TMemo
117119
Left = 24
118-
Top = 247
120+
Top = 262
119121
Width = 601
120122
Height = 34
121123
ScrollBars = ssVertical
@@ -130,10 +132,6 @@ object C4DValidateComponentsDemo01ViewMain: TC4DValidateComponentsDemo01ViewMain
130132
TabOrder = 7
131133
object TabSheet1: TTabSheet
132134
Caption = 'TabSheet1'
133-
ExplicitLeft = 0
134-
ExplicitTop = 0
135-
ExplicitWidth = 0
136-
ExplicitHeight = 0
137135
object Edit2: TEdit
138136
Left = 32
139137
Top = 32
@@ -150,15 +148,11 @@ object C4DValidateComponentsDemo01ViewMain: TC4DValidateComponentsDemo01ViewMain
150148
Top = 0
151149
Width = 545
152150
Height = 103
153-
ActivePage = TabSheet4
151+
ActivePage = TabSheet3
154152
Align = alClient
155153
TabOrder = 0
156154
object TabSheet3: TTabSheet
157155
Caption = 'TabSheet3'
158-
ExplicitLeft = 0
159-
ExplicitTop = 0
160-
ExplicitWidth = 0
161-
ExplicitHeight = 0
162156
object Edit1: TEdit
163157
Left = 32
164158
Top = 32
@@ -212,10 +206,19 @@ object C4DValidateComponentsDemo01ViewMain: TC4DValidateComponentsDemo01ViewMain
212206
end
213207
end
214208
end
209+
object btnClearAllFields: TButton
210+
Left = 28
211+
Top = 489
212+
Width = 75
213+
Height = 25
214+
Caption = 'Clear all fields'
215+
TabOrder = 8
216+
OnClick = btnClearAllFieldsClick
217+
end
215218
end
216219
object pnBotoes: TPanel
217220
Left = 0
218-
Top = 488
221+
Top = 543
219222
Width = 662
220223
Height = 45
221224
Align = alBottom
@@ -224,6 +227,7 @@ object C4DValidateComponentsDemo01ViewMain: TC4DValidateComponentsDemo01ViewMain
224227
Padding.Right = 3
225228
Padding.Bottom = 3
226229
TabOrder = 1
230+
ExplicitTop = 488
227231
object btnValidar: TButton
228232
Left = 4
229233
Top = 4
@@ -233,6 +237,8 @@ object C4DValidateComponentsDemo01ViewMain: TC4DValidateComponentsDemo01ViewMain
233237
Caption = 'Validar'
234238
TabOrder = 0
235239
OnClick = btnValidarClick
240+
ExplicitLeft = 0
241+
ExplicitTop = 6
236242
end
237243
end
238244
end

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

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ TC4DValidateComponentsDemo01ViewMain = class(TForm)
3333
edtCodigo: TEdit;
3434

3535
[NotEmpty]
36-
[Length(2, 5)]
37-
//'Texto deve ter entre <min> e <max> caracteres'
36+
[Length(5, 10)] //'Texto deve ter entre <min> e <max> caracteres'
3837
edtNome: TEdit;
3938

39+
4040
[NotEmpty]
41-
[MinMaxValue(5, 50)]
41+
[MinMaxValue(5, 10)]
4242
edtLimite: TEdit;
4343

4444
[NotEmpty]
@@ -50,10 +50,12 @@ TC4DValidateComponentsDemo01ViewMain = class(TForm)
5050
[NotEmpty]
5151
edtTesteComAbas: TEdit;
5252

53+
[NotEmpty]
54+
Memo1: TMemo;
55+
5356
ckAtivo: TCheckBox;
5457
pnBotoes: TPanel;
5558
btnValidar: TButton;
56-
Memo1: TMemo;
5759
PageControl1: TPageControl;
5860
TabSheet1: TTabSheet;
5961
Edit2: TEdit;
@@ -65,7 +67,9 @@ TC4DValidateComponentsDemo01ViewMain = class(TForm)
6567
Panel1: TPanel;
6668
GroupBox1: TGroupBox;
6769
Label5: TLabel;
70+
btnClearAllFields: TButton;
6871
procedure btnValidarClick(Sender: TObject);
72+
procedure btnClearAllFieldsClick(Sender: TObject);
6973
private
7074
public
7175
end;
@@ -77,14 +81,33 @@ implementation
7781

7882
{$R *.dfm}
7983

84+
procedure TC4DValidateComponentsDemo01ViewMain.btnClearAllFieldsClick(Sender: TObject);
85+
var
86+
i: Integer;
87+
LComponent: TComponent;
88+
begin
89+
for i := 0 to Pred(Self.ComponentCount) do
90+
begin
91+
LComponent := Self.Components[i];
92+
if(LComponent is TEdit)then
93+
TEdit(LComponent).Clear
94+
else if(LComponent is TComboBox)then
95+
TComboBox(LComponent).ItemIndex := -1
96+
else if(LComponent is TCheckBox)then
97+
TCheckBox(LComponent).Checked := False
98+
else if(LComponent is TRadioGroup)then
99+
TRadioGroup(LComponent).ItemIndex := -1
100+
else if(LComponent is TMemo)then
101+
TMemo(LComponent).Lines.Clear
102+
end;
103+
end;
80104

81105
procedure TC4DValidateComponentsDemo01ViewMain.btnValidarClick(Sender: TObject);
82106
var
83107
LRttiContext: TRttiContext;
84108
LRttiType: TRttiType;
85109
LRttiField: TRttiField;
86110
LCustomAttribute: TCustomAttribute;
87-
LComponent: TComponent;
88111
begin
89112
LRttiContext := TRttiContext.Create;
90113
try

Src/C4D.Validate.Components.Components.pas

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,28 @@ implementation
2424
class function TC4DValidateComponentsComponents.GetTextFromComponent(const AComponent: TComponent): string;
2525
begin
2626
Result := '';
27-
if(AComponent is TEdit)then
28-
Result := Trim((AComponent as TEdit).Text)
29-
else if(AComponent is TComboBox)then
30-
Result := Trim((AComponent as TComboBox).Text)
31-
else if(AComponent is TMemo)then
32-
Result := Trim((AComponent as TMemo).Text)
33-
else if(AComponent is TRadioGroup)then
34-
begin
35-
if(TRadioGroup(AComponent).ItemIndex >= 0)then
36-
Result := TRadioGroup(AComponent).Items[TRadioGroup(AComponent).ItemIndex];
27+
try
28+
if(AComponent is TEdit)then
29+
Exit(TEdit(AComponent).Text);
30+
31+
if(AComponent is TComboBox)then
32+
Exit(TComboBox(AComponent).Text);
33+
34+
if(AComponent is TMemo)then
35+
Exit(TMemo(AComponent).Text);
36+
37+
if(AComponent is TRadioGroup)then
38+
begin
39+
if(TRadioGroup(AComponent).ItemIndex >= 0)then
40+
Result := TRadioGroup(AComponent).Items[TRadioGroup(AComponent).ItemIndex];
41+
42+
Exit;
43+
end;
44+
45+
if(AComponent is TDateTimePicker)then
46+
Exit(FloatToStr(TDateTimePicker(AComponent).Date));
47+
finally
48+
Result := Result.Trim;
3749
end;
3850
end;
3951

Src/C4D.Validate.Components.NotEmpty.pas

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,27 @@ interface
1111
type
1212
NotEmpty = class(TCustomAttribute)
1313
private
14+
FMsg: string;
1415
public
16+
constructor Create; overload;
17+
constructor Create(const AMsg: string); overload;
1518
procedure Validar(const ARttiField: TRttiField; const AObject: TObject);
1619
end;
1720

1821
implementation
1922

2023
const
21-
MSG_OBG = 'Campo obrigatório sem preenchimento';
24+
MSG_PADRAO = 'Campo obrigatório sem preenchimento';
25+
26+
constructor NotEmpty.Create;
27+
begin
28+
FMsg := MSG_PADRAO;
29+
end;
30+
31+
constructor NotEmpty.Create(const AMsg: string);
32+
begin
33+
FMsg := AMsg;
34+
end;
2235

2336
procedure NotEmpty.Validar(const ARttiField: TRttiField; const AObject: TObject);
2437
var
@@ -31,7 +44,7 @@ procedure NotEmpty.Validar(const ARttiField: TRttiField; const AObject: TObject)
3144
if(LText.Trim.IsEmpty)then
3245
begin
3346
TC4DValidateComponentsComponents.SetFocu(LComponent);
34-
raise Exception.Create(MSG_OBG);
47+
raise Exception.Create(FMsg);
3548
end;
3649
end;
3750

0 commit comments

Comments
 (0)