-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: [Validation] add support for dbGroup as parameter in is_unique and is_not_unique #9216
Open
maniaba
wants to merge
5
commits into
codeigniter4:4.6
Choose a base branch
from
maniaba:dev.9215
base: 4.6
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+99
−38
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
use CodeIgniter\Helpers\Array\ArrayHelper; | ||
use CodeIgniter\Validation\Rules as NonStrictRules; | ||
use Config\Database; | ||
use InvalidArgumentException; | ||
|
||
/** | ||
* Validation Rules. | ||
|
@@ -134,6 +135,7 @@ public function greater_than_equal_to($str, string $min): bool | |
* accept only one filter). | ||
* | ||
* Example: | ||
* is_not_unique[dbGroup.table.field,where_field,where_value] | ||
* is_not_unique[table.field,where_field,where_value] | ||
* is_not_unique[menu.id,active,1] | ||
* | ||
|
@@ -146,16 +148,21 @@ public function is_not_unique($str, string $field, array $data): bool | |
} | ||
|
||
// Grab any data for exclusion of a single row. | ||
[$field, $whereField, $whereValue] = array_pad( | ||
explode(',', $field), | ||
3, | ||
null | ||
); | ||
|
||
// Break the table and field apart | ||
sscanf($field, '%[^.].%[^.]', $table, $field); | ||
[$field, $whereField, $whereValue] = array_pad(explode(',', $field), 3, null); | ||
|
||
// Break the dbGroup, table and field apart from the field string | ||
$parts = explode('.', $field, 3); | ||
$numParts = count($parts); | ||
|
||
if ($numParts === 3) { | ||
[$dbGroup, $table, $field] = $parts; | ||
} elseif ($numParts === 2) { | ||
[$table, $field] = $parts; | ||
} else { | ||
throw new InvalidArgumentException('The field must be in the format "table.field" or "dbGroup.table.field".'); | ||
} | ||
|
||
$row = Database::connect($data['DBGroup'] ?? null) | ||
$row = Database::connect($dbGroup ?? $data['DBGroup'] ?? null) | ||
->table($table) | ||
->select('1') | ||
->where($field, $str) | ||
|
@@ -196,6 +203,7 @@ public function in_list($value, string $list): bool | |
* record updates. | ||
* | ||
* Example: | ||
* is_unique[dbGroup.table.field,ignore_field,ignore_value] | ||
* is_unique[table.field,ignore_field,ignore_value] | ||
* is_unique[users.email,id,5] | ||
* | ||
|
@@ -207,15 +215,23 @@ public function is_unique($str, string $field, array $data): bool | |
return false; | ||
} | ||
|
||
[$field, $ignoreField, $ignoreValue] = array_pad( | ||
explode(',', $field), | ||
3, | ||
null | ||
); | ||
|
||
sscanf($field, '%[^.].%[^.]', $table, $field); | ||
// Grab any data for exclusion of a single row. | ||
[$field, $ignoreField, $ignoreValue] = array_pad(explode(',', $field), 3, null); | ||
|
||
// Break the dbGroup, table and field apart from the field string | ||
// Break the dbGroup, table and field apart from the field string | ||
Comment on lines
+221
to
+222
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Double comment. |
||
$parts = explode('.', $field, 3); | ||
$numParts = count($parts); | ||
|
||
if ($numParts === 3) { | ||
[$dbGroup, $table, $field] = $parts; | ||
} elseif ($numParts === 2) { | ||
[$table, $field] = $parts; | ||
} else { | ||
throw new InvalidArgumentException('The field must be in the format "table.field" or "dbGroup.table.field".'); | ||
} | ||
|
||
$row = Database::connect($data['DBGroup'] ?? null) | ||
$row = Database::connect($dbGroup ?? $data['DBGroup'] ?? null) | ||
->table($table) | ||
->select('1') | ||
->where($field, $str) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -219,6 +219,8 @@ Libraries | |||||||||
See :ref:`FileCollection::retainMultiplePatterns() <file-collections-retain-multiple-patterns>`. | ||||||||||
- **Validation:** Added ``min_dims`` validation rule to ``FileRules`` class. See | ||||||||||
:ref:`Validation <rules-for-file-uploads>`. | ||||||||||
- **Validation:** Rule ``is_unique`` and ``is_not_unique`` now accept an optional | ||||||||||
``dbGroup`` as parameter. See :ref:`Validation <rules-for-general-use>`. | ||||||||||
Comment on lines
+222
to
+223
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
Helpers and Functions | ||||||||||
===================== | ||||||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add wrong validation test for exception