Skip to content

Commit 3ff20f1

Browse files
author
César Cardoso
committed
feature/add-FieldDisplay-e-helpers
1 parent 9bc58a1 commit 3ff20f1

9 files changed

Lines changed: 114 additions & 13 deletions

Samples/Demo01/C4DValidateComponentsDemo01.dpr

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ uses
77
C4D.Validate.Components.NotEmpty in '..\..\Src\C4D.Validate.Components.NotEmpty.pas',
88
C4D.Validate.Components.Length in '..\..\Src\C4D.Validate.Components.Length.pas',
99
C4D.Validate.Components.MinMaxValue in '..\..\Src\C4D.Validate.Components.MinMaxValue.pas',
10-
C4D.Validate.Components in '..\..\Src\C4D.Validate.Components.pas';
10+
C4D.Validate.Components in '..\..\Src\C4D.Validate.Components.pas',
11+
C4D.Validate.Components.FieldDisplay in '..\..\Src\C4D.Validate.Components.FieldDisplay.pas',
12+
C4D.Validate.Components.Helpers in '..\..\Src\C4D.Validate.Components.Helpers.pas';
1113

1214
{$R *.res}
1315

Samples/Demo01/C4DValidateComponentsDemo01.dproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@
107107
<DCCReference Include="..\..\Src\C4D.Validate.Components.Length.pas"/>
108108
<DCCReference Include="..\..\Src\C4D.Validate.Components.MinMaxValue.pas"/>
109109
<DCCReference Include="..\..\Src\C4D.Validate.Components.pas"/>
110+
<DCCReference Include="..\..\Src\C4D.Validate.Components.FieldDisplay.pas"/>
111+
<DCCReference Include="..\..\Src\C4D.Validate.Components.Helpers.pas"/>
110112
<BuildConfiguration Include="Release">
111113
<Key>Cfg_2</Key>
112114
<CfgParent>Base</CfgParent>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ object C4DValidateComponentsDemo01ViewMain: TC4DValidateComponentsDemo01ViewMain
1111
Font.Name = 'Tahoma'
1212
Font.Style = []
1313
OldCreateOrder = False
14+
Position = poScreenCenter
1415
PixelsPerInch = 96
1516
TextHeight = 13
1617
object pnTudo: TPanel

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ interface
1414
Vcl.Dialogs,
1515
Vcl.StdCtrls,
1616
Vcl.ExtCtrls,
17+
Vcl.ComCtrls,
1718
System.RTTI,
1819
C4D.Validate.Components.NotEmpty,
1920
C4D.Validate.Components.Length,
2021
C4D.Validate.Components.MinMaxValue,
21-
Vcl.ComCtrls;
22+
C4D.Validate.Components.FieldDisplay;
2223

2324
type
2425
TC4DValidateComponentsDemo01ViewMain = class(TForm)
@@ -29,14 +30,15 @@ TC4DValidateComponentsDemo01ViewMain = class(TForm)
2930
Label3: TLabel;
3031
Label4: TLabel;
3132

32-
[NotEmpty]
33+
[FieldDisplay('Código')]
34+
[NotEmpty('Campo <FieldDisplay> não pode ficar vazio')]
3335
edtCodigo: TEdit;
3436

37+
[FieldDisplay('Nome')]
3538
[NotEmpty]
3639
[Length(5, 10)] //'Texto deve ter entre <min> e <max> caracteres'
3740
edtNome: TEdit;
3841

39-
4042
[NotEmpty]
4143
[MinMaxValue(5, 10)]
4244
edtLimite: TEdit;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
unit C4D.Validate.Components.FieldDisplay;
2+
3+
interface
4+
5+
uses
6+
System.SysUtils;
7+
8+
type
9+
FieldDisplay = class(TCustomAttribute)
10+
private
11+
FDisplay: String;
12+
public
13+
constructor Create(const ADisplay: String);
14+
property Display: String read FDisplay;
15+
end;
16+
17+
implementation
18+
19+
constructor FieldDisplay.Create(const ADisplay: String);
20+
begin
21+
FDisplay := ADisplay;
22+
end;
23+
24+
end.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
unit C4D.Validate.Components.Helpers;
2+
3+
interface
4+
5+
uses
6+
System.SysUtils,
7+
System.RTTI,
8+
System.TypInfo;
9+
10+
type
11+
TRttiFieldyHelper = class helper for TRttiField
12+
private
13+
function GetCustomAttribute<T: TCustomAttribute>: T;
14+
public
15+
function GetFieldDisplay: string;
16+
function FormatMsg(const AMsg: string): string;
17+
end;
18+
19+
implementation
20+
21+
uses
22+
C4D.Validate.Components.FieldDisplay;
23+
24+
function TRttiFieldyHelper.GetCustomAttribute<T>: T;
25+
var
26+
LCustomAttribute: TCustomAttribute;
27+
begin
28+
Result := nil;
29+
for LCustomAttribute in GetAttributes do
30+
if LCustomAttribute is T then
31+
Exit((LCustomAttribute as T));
32+
end;
33+
34+
function TRttiFieldyHelper.GetFieldDisplay: string;
35+
var
36+
LFieldDisplay: FieldDisplay;
37+
begin
38+
Result := ''; //Name;
39+
LFieldDisplay := GetCustomAttribute<FieldDisplay>;
40+
if(LFieldDisplay <> nil)then
41+
Result := LFieldDisplay.Display; //Format(' [%s]', [LFieldDisplay.Display]) ;
42+
end;
43+
44+
function TRttiFieldyHelper.FormatMsg(const AMsg: string): string;
45+
const
46+
TAG = '<FieldDisplay>';
47+
var
48+
LFieldDisplay: string;
49+
begin
50+
Result := AMsg;
51+
LFieldDisplay := Self.GetFieldDisplay;
52+
53+
if(pos(TAG, AMsg) > 0)then
54+
Result := AMsg.Replace(TAG, LFieldDisplay, [rfReplaceAll, rfIgnoreCase])
55+
else if(not LFieldDisplay.Trim.IsEmpty)then
56+
Result := AMsg + Format(' [%s]', [Self.GetFieldDisplay]) ;
57+
end;
58+
59+
60+
end.

Src/C4D.Validate.Components.Length.pas

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ interface
55
uses
66
System.SysUtils,
77
System.Classes,
8-
System.RTTI,
9-
C4D.Validate.Components.Components;
8+
System.RTTI;
109

1110
type
1211
Length = class(TCustomAttribute)
@@ -22,6 +21,10 @@ Length = class(TCustomAttribute)
2221

2322
implementation
2423

24+
uses
25+
C4D.Validate.Components.Helpers,
26+
C4D.Validate.Components.Components;
27+
2528
const
2629
MSG_PADRAO = 'Campo deve ter entre <min> e <max> caracteres';
2730
MSG_PADRAO_MIN = 'Campo deve ter ao menos <min> caracteres';
@@ -61,7 +64,7 @@ procedure Length.Validar(const ARttiField: TRttiField; const AObject: TObject);
6164
if(LLength < FMinLength)or((FMaxLength > 0)and(LLength > FMaxLength))then
6265
begin
6366
TC4DValidateComponentsComponents.SetFocu(LComponent);
64-
raise Exception.Create(Self.GetMsg);
67+
raise Exception.Create(ARttiField.FormatMsg(Self.GetMsg));
6568
end;
6669
end;
6770

Src/C4D.Validate.Components.MinMaxValue.pas

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ interface
55
uses
66
System.SysUtils,
77
System.Classes,
8-
System.RTTI,
9-
C4D.Validate.Components.Components;
8+
System.RTTI;
109

1110
type
1211
MinMaxValue = class(TCustomAttribute)
@@ -22,6 +21,10 @@ MinMaxValue = class(TCustomAttribute)
2221

2322
implementation
2423

24+
uses
25+
C4D.Validate.Components.Helpers,
26+
C4D.Validate.Components.Components;
27+
2528
const
2629
MSG_PADRAO = 'Campo deve ter um valor entre <min> e <max>';
2730
MSG_PADRAO_MIN = 'Campo deve ter um valor mínimo de <min>';
@@ -61,7 +64,7 @@ procedure MinMaxValue.Validar(const ARttiField: TRttiField; const AObject: TObje
6164
if(LValue < FMinValue)or((FMaxValue > 0)and(LValue > FMaxValue))then
6265
begin
6366
TC4DValidateComponentsComponents.SetFocu(LComponent);
64-
raise Exception.Create(Self.GetMsg);
67+
raise Exception.Create(Self.GetMsg + ARttiField.GetFieldDisplay);
6568
end;
6669
end;
6770

Src/C4D.Validate.Components.NotEmpty.pas

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ interface
55
uses
66
System.SysUtils,
77
System.Classes,
8-
System.RTTI,
9-
C4D.Validate.Components.Components;
8+
System.RTTI;
109

1110
type
1211
NotEmpty = class(TCustomAttribute)
@@ -20,6 +19,10 @@ NotEmpty = class(TCustomAttribute)
2019

2120
implementation
2221

22+
uses
23+
C4D.Validate.Components.Helpers,
24+
C4D.Validate.Components.Components;
25+
2326
const
2427
MSG_PADRAO = 'Campo obrigatório sem preenchimento';
2528

@@ -44,7 +47,8 @@ procedure NotEmpty.Validar(const ARttiField: TRttiField; const AObject: TObject)
4447
if(LText.Trim.IsEmpty)then
4548
begin
4649
TC4DValidateComponentsComponents.SetFocu(LComponent);
47-
raise Exception.Create(FMsg);
50+
//raise Exception.Create(FMsg + ARttiField.GetFieldDisplay);
51+
raise Exception.Create(ARttiField.FormatMsg(FMsg));
4852
end;
4953
end;
5054

0 commit comments

Comments
 (0)