|
| 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. |
0 commit comments