1111
1212namespace CodeIgniter \Validation ;
1313
14- use CodeIgniter \Validation \StrictRules \Rules as StrictRules ;
1514use Config \Database ;
1615use InvalidArgumentException ;
1716
2019 */
2120class Rules
2221{
23- private ?StrictRules $ strictRules = null ;
24-
25- private function createStrictRules (): void
26- {
27- if ($ this ->strictRules === null ) {
28- $ this ->strictRules = new StrictRules ();
29- }
30- }
31-
3222 /**
3323 * The value does not match another field in $data.
3424 *
@@ -95,9 +85,23 @@ public function greater_than_equal_to(?string $str, string $min): bool
9585 */
9686 public function is_not_unique (?string $ str , string $ field , array $ data ): bool
9787 {
98- $ this ->createStrictRules ();
88+ // Grab any data for exclusion of a single row.
89+ [$ field , $ whereField , $ whereValue ] = array_pad (explode (', ' , $ field ), 3 , null );
90+
91+ // Break the table and field apart
92+ sscanf ($ field , '%[^.].%[^.] ' , $ table , $ field );
93+
94+ $ row = Database::connect ($ data ['DBGroup ' ] ?? null )
95+ ->table ($ table )
96+ ->select ('1 ' )
97+ ->where ($ field , $ str )
98+ ->limit (1 );
9999
100- return $ this ->strictRules ->is_not_unique ($ str , $ field , $ data );
100+ if (! empty ($ whereField ) && ! empty ($ whereValue ) && ! preg_match ('/^\{(\w+)\}$/ ' , $ whereValue )) {
101+ $ row = $ row ->where ($ whereField , $ whereValue );
102+ }
103+
104+ return $ row ->get ()->getRow () !== null ;
101105 }
102106
103107 /**
@@ -121,9 +125,21 @@ public function in_list(?string $value, string $list): bool
121125 */
122126 public function is_unique (?string $ str , string $ field , array $ data ): bool
123127 {
124- $ this ->createStrictRules ();
128+ [$ field , $ ignoreField , $ ignoreValue ] = array_pad (explode (', ' , $ field ), 3 , null );
129+
130+ sscanf ($ field , '%[^.].%[^.] ' , $ table , $ field );
131+
132+ $ row = Database::connect ($ data ['DBGroup ' ] ?? null )
133+ ->table ($ table )
134+ ->select ('1 ' )
135+ ->where ($ field , $ str )
136+ ->limit (1 );
137+
138+ if (! empty ($ ignoreField ) && ! empty ($ ignoreValue ) && ! preg_match ('/^\{(\w+)\}$/ ' , $ ignoreValue )) {
139+ $ row = $ row ->where ("{$ ignoreField } != " , $ ignoreValue );
140+ }
125141
126- return $ this -> strictRules -> is_unique ( $ str , $ field , $ data ) ;
142+ return $ row -> get ()-> getRow () === null ;
127143 }
128144
129145 /**
0 commit comments