Skip to content
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

[5.6] Ignore a given model during a unique check. #23524

Merged
merged 3 commits into from
Mar 13, 2018
Merged

[5.6] Ignore a given model during a unique check. #23524

merged 3 commits into from
Mar 13, 2018

Conversation

Alymosul
Copy link
Contributor

This PR adds the ability to ignore a given model during a unique validation rule check instead of passing the column's value and name to the ignore() method, we can pass the actual model to the ignore and by default the rule will grab the primary key name and value for the model. Also it adds the ability to select a custom column and the corresponding value will be retrieved from the model.

Let's Consider we have a Company model that we need to update it's name and the company names should be unique, therefore the validation rule will be as follows

Currently:

$rules = [
            'name' => [
                'required',
                Rule::unique('companies', 'name')->ignore($this->company->id);
            ],
        ];

// Let's say we need to select a custom idColumn

$rules = [
            'name' => [
                'required',
                Rule::unique('companies', 'name')->ignore($this->company->slug, 'slug');
            ],
        ];
Proposed:

$rules = [
            'name' => [
                'required',
                Rule::unique('companies', 'name')->ignore($this->company);
            ],
        ];

// Let's say we need to select a custom idColumn

$rules = [
            'name' => [
                'required',
                Rule::unique('companies', 'name')->ignore($this->company, 'slug');
            ],
        ];

// Also we can use more expressive method

$rules = [
            'name' => [
                'required',
                Rule::unique('companies', 'name')->ignoreModel($this->company);
            ],
        ];

Note:

this line $this->idColumn = $idColumn?? 'id'; has been added to the ignore() method to avoid breaking old laravel releases.

@@ -46,7 +67,7 @@ public function __toString()
$this->table,
$this->column,
$this->ignore ? '"'.$this->ignore.'"' : 'NULL',
$this->idColumn,
$this->idColumn ?? 'NULL',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain why this is needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because we removed the default value for the protected $idColumn; and the __toString() need string conversion for the variables

@@ -0,0 +1 @@
hi
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Contributor Author

@Alymosul Alymosul Mar 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know, it was wrongly added. Sorry for that.


class ValidationUniqueRuleTest extends TestCase
{
public function testItCorrectlyFormatsAStringVersionOfTheRule()
{
$rule = new \Illuminate\Validation\Rules\Unique('table');
$rule->where('foo', 'bar');
$this->assertEquals('unique:table,NULL,NULL,id,foo,bar', (string) $rule);
$this->assertEquals('unique:table,NULL,NULL,NULL,foo,bar', (string) $rule);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes me uncomfortable having to change an old test to add this feature.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we don't need the default value for the idColumn that's why i had to change the test here, because we really don't need the 'id' since the ignore is NULL. but I can revert this change and still the feature will pass.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK please don't change that test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, Sorry for that 👍

@taylorotwell taylorotwell merged commit 475c138 into laravel:5.6 Mar 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants