File tree Expand file tree Collapse file tree 3 files changed +75
-0
lines changed Expand file tree Collapse file tree 3 files changed +75
-0
lines changed Original file line number Diff line number Diff line change 22
33namespace App \Models ;
44
5+ use App \Rules \AlphaEncrypted ;
6+ use App \Rules \NumericEncrypted ;
57use Gate ;
68use Illuminate \Database \Eloquent \Factories \HasFactory ;
79use Illuminate \Database \Eloquent \Model ;
@@ -95,6 +97,19 @@ public function validation_rules()
9597 array_push ($ rule , $ field ->attributes ['format ' ]);
9698 $ rules [$ field ->db_column_name ()] = $ rule ;
9799
100+
101+ // these are to replace the standard 'numeric' and 'alpha' rules if the custom field is also encrypted.
102+ // the values need to be decrypted first, because encrypted strings are alphanumeric
103+ if ($ field ->format === 'NUMERIC ' && $ field ->field_encrypted ) {
104+ $ numericKey = array_search ('numeric ' , $ rules [$ field ->db_column_name ()]);
105+ $ rules [$ field ->db_column_name ()][$ numericKey ] = new NumericEncrypted ;
106+ }
107+
108+ if ($ field ->format === 'ALPHA ' && $ field ->field_encrypted ) {
109+ $ alphaKey = array_search ('alpha ' , $ rules [$ field ->db_column_name ()]);
110+ $ rules [$ field ->db_column_name ()][$ alphaKey ] = new AlphaEncrypted ;
111+ }
112+
98113 // add not_array to rules for all fields but checkboxes
99114 if ($ field ->element != 'checkbox ' ) {
100115 $ rules [$ field ->db_column_name ()][] = 'not_array ' ;
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace App \Rules ;
4+
5+ use Closure ;
6+ use Illuminate \Contracts \Validation \ValidationRule ;
7+ use Illuminate \Support \Facades \Crypt ;
8+
9+ class AlphaEncrypted implements ValidationRule
10+ {
11+ /**
12+ * Run the validation rule.
13+ *
14+ * @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
15+ */
16+ public function validate (string $ attribute , mixed $ value , Closure $ fail ): void
17+ {
18+ try {
19+ $ attributeName = trim (preg_replace ('/_+|snipeit|\d+/ ' , ' ' , $ attribute ));
20+ $ decrypted = Crypt::decrypt ($ value );
21+ if (!ctype_alpha ($ decrypted ) && !is_null ($ decrypted )) {
22+ $ fail (trans ('validation.alpha ' , ['attribute ' => $ attributeName ]));
23+ }
24+ } catch (\Exception $ e ) {
25+ report ($ e );
26+ $ fail (trans ('general.something_went_wrong ' ));
27+ }
28+ }
29+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace App \Rules ;
4+
5+ use Closure ;
6+ use Illuminate \Contracts \Encryption \DecryptException ;
7+ use Illuminate \Contracts \Validation \ValidationRule ;
8+ use Illuminate \Support \Facades \Crypt ;
9+
10+ class NumericEncrypted implements ValidationRule
11+ {
12+ /**
13+ * Run the validation rule.
14+ *
15+ * @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
16+ */
17+ public function validate (string $ attribute , mixed $ value , Closure $ fail ): void
18+ {
19+
20+ try {
21+ $ attributeName = trim (preg_replace ('/_+|snipeit|\d+/ ' , ' ' , $ attribute ));
22+ $ decrypted = Crypt::decrypt ($ value );
23+ if (!is_numeric ($ decrypted ) && !is_null ($ decrypted )) {
24+ $ fail (trans ('validation.numeric ' , ['attribute ' => $ attributeName ]));
25+ }
26+ } catch (\Exception $ e ) {
27+ report ($ e ->getMessage ());
28+ $ fail (trans ('general.something_went_wrong ' ));
29+ }
30+ }
31+ }
You can’t perform that action at this time.
0 commit comments