Skip to content

Commit

Permalink
Fixes connection name using unique validation rule
Browse files Browse the repository at this point in the history
  • Loading branch information
octoberapp committed Nov 6, 2023
1 parent 3c2791d commit 394adc9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 31 deletions.
29 changes: 15 additions & 14 deletions src/Database/Traits/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -491,14 +491,15 @@ protected function processRuleFieldNames($rules)
*/
protected function processValidationUniqueRule($definition, $fieldName)
{
[$table, $column, $key, $keyName, $whereColumn, $whereValue] = array_pad(explode(',', $definition, 6), 6, null);
[$ruleName, $ruleDefinition] = array_pad(explode(':', $definition, 2), 2, '');
[$tableName, $column, $key, $keyName, $whereColumn, $whereValue] = array_pad(explode(',', $ruleDefinition, 6), 6, null);

$table = 'unique:' . $this->getTable();
$tableName = str_contains($tableName, '.') ? $tableName : $this->getTable();
$column = $column ?: $fieldName;
$key = $keyName ? $this->$keyName : $this->getKey();
$keyName = $keyName ?: $this->getKeyName();

$params = [$table, $column, $key, $keyName];
$params = [$tableName, $column, $key, $keyName];

if ($whereColumn) {
$params[] = $whereColumn;
Expand All @@ -508,7 +509,7 @@ protected function processValidationUniqueRule($definition, $fieldName)
$params[] = $whereValue;
}

return implode(',', $params);
return $ruleName . ':' . implode(',', $params);
}

/**
Expand All @@ -526,26 +527,26 @@ public function isAttributeRequired($attribute, $checkDependencies = true)
return false;
}

$ruleset = $this->rules[$attribute];
$ruleSet = $this->rules[$attribute];

if (is_array($ruleset)) {
$ruleset = implode('|', $ruleset);
if (is_array($ruleSet)) {
$ruleSet = implode('|', $ruleSet);
}

if (strpos($ruleset, 'required:create') !== false && $this->exists) {
if (strpos($ruleSet, 'required:create') !== false && $this->exists) {
return false;
}

if (strpos($ruleset, 'required:update') !== false && !$this->exists) {
if (strpos($ruleSet, 'required:update') !== false && !$this->exists) {
return false;
}

if (strpos($ruleset, 'required_with') !== false) {
if (strpos($ruleSet, 'required_with') !== false) {
if (!$checkDependencies) {
return true;
}

$requiredWith = substr($ruleset, strpos($ruleset, 'required_with') + 14);
$requiredWith = substr($ruleSet, strpos($ruleSet, 'required_with') + 14);

if (strpos($requiredWith, '|') !== false) {
$requiredWith = substr($requiredWith, 0, strpos($requiredWith, '|'));
Expand All @@ -554,18 +555,18 @@ public function isAttributeRequired($attribute, $checkDependencies = true)
return $this->isAttributeRequired($requiredWith, false);
}

if (strpos($ruleset, 'required_if') !== false) {
if (strpos($ruleSet, 'required_if') !== false) {
if (!$checkDependencies) {
return true;
}

$requiredIf = substr($ruleset, strpos($ruleset, 'required_if') + 12);
$requiredIf = substr($ruleSet, strpos($ruleSet, 'required_if') + 12);
$requiredIf = substr($requiredIf, 0, strpos($requiredIf, ','));

return $this->isAttributeRequired($requiredIf, false);
}

return strpos($ruleset, 'required') !== false;
return strpos($ruleSet, 'required') !== false;
}

/**
Expand Down
42 changes: 25 additions & 17 deletions tests/Database/Traits/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ class ValidationTest extends TestCase

public $id = 20;

public function testUnique()
public function testUniqueRule()
{
/*
* Basic usage of unique rule
*/
// Basic usage of unique rule
$rules = ['email' => 'unique:users'];

$this->exists = true;
Expand All @@ -25,9 +23,20 @@ public function testUnique()
'email' => ['unique:users']
], $this->processValidationRules($rules));

/*
* Specifying a custom column name
*/
// Custom database connection
$rules = ['email' => 'unique:myconnection.users'];

$this->exists = true;
$this->assertEquals([
'email' => ['unique:myconnection.users,email,7,the_id']
], $this->processValidationRules($rules));

$this->exists = false;
$this->assertEquals([
'email' => ['unique:myconnection.users']
], $this->processValidationRules($rules));

// Custom table column name
$rules = ['email' => 'unique:users,email_address'];

$this->exists = true;
Expand All @@ -40,9 +49,7 @@ public function testUnique()
'email' => ['unique:users,email_address']
], $this->processValidationRules($rules));

/*
* Forcing a unique rule to ignore a given ID
*/
// Forcing a unique rule to ignore a given ID
$rules = ['email' => 'unique:users,email_address,10'];

$this->exists = true;
Expand All @@ -55,9 +62,7 @@ public function testUnique()
'email' => ['unique:users,email_address,10']
], $this->processValidationRules($rules));

/*
* Adding additional where clauses
*/
// Adding additional where clauses
$rules = ['email' => 'unique:users,email_address,NULL,id,account_id,1'];

$this->exists = true;
Expand All @@ -70,9 +75,7 @@ public function testUnique()
'email' => ['unique:users,email_address,NULL,id,account_id,1']
], $this->processValidationRules($rules));

/*
* Adding multiple additional where clauses
*/
// Adding multiple additional where clauses
$rules = ['email' => 'unique:users,email_address,NULL,id,account_id,1,account_name,"Foo",user_id,3'];

$this->exists = true;
Expand All @@ -91,6 +94,11 @@ protected function getTable()
return 'users';
}

protected function getConnectionName()
{
return 'mysql';
}

protected function getKey()
{
return 7;
Expand All @@ -103,7 +111,7 @@ protected function getKeyName()

public function testArrayFieldNames()
{
$mock = $this->getMockForTrait('October\Rain\Database\Traits\Validation');
$mock = $this->getMockForTrait(\October\Rain\Database\Traits\Validation::class);

$rules = [
'field' => 'required',
Expand Down

0 comments on commit 394adc9

Please sign in to comment.