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.2] Cast unique validation exclude id to integer #14076

Merged
merged 2 commits into from
Jun 20, 2016

Conversation

sakalauskas
Copy link
Contributor

@sakalauskas sakalauskas commented Jun 20, 2016

I have this quite complex validation rule and I use with mongodb:

unique:mongodb.responses,RY7bC4VBFdzFWBS3.age,123,visitor_id,survey_id,564ce591c560ca2c6a8b4568

The problem occurs that in MongoDB where = 123 and where = '123' are two different queries, where in MySQL there is no difference.

Therefore I really need to cast ID attribute to integer to have it return the correct count for unique validation and I suppose that it can only be done from here.

@gpedro
Copy link

gpedro commented Jun 20, 2016

I may be wrong, but if you add integer it does the cast?

integer | unique:mongodb.responses, ...

@sakalauskas
Copy link
Contributor Author

@gpedro Would be great if it worked. But your suggested solution adds validation to the field, but does not cast the except in unique:table,column,except,idColumn rule definition.

@GrahamCampbell GrahamCampbell changed the title Cast unique validation exclude id to integer [5.2] Cast unique validation exclude id to integer Jun 20, 2016
@sakalauskas
Copy link
Contributor Author

Thanks @GrahamCampbell for updating title, didn't knew to which branch (5.1 or 5.2 or 5.3) to tag this PR.

@taylorotwell
Copy link
Member

What if someone is using UUIDs?

@sakalauskas
Copy link
Contributor Author

sakalauskas commented Jun 20, 2016

@taylorotwell Well, if its really a string, then do not cast it and leave like it is?

I've added conditional check to check if it is really an integer.
if (filter_var($id, FILTER_VALIDATE_INT) !== false)

Don't know how I can better check for casting.

Would prefixing 'int:' the id might be better?

e.g.
if (starts_with($id, 'int:')) { $id = intval($id); }

@taylorotwell taylorotwell reopened this Jun 20, 2016
@taylorotwell taylorotwell merged commit a94a83c into laravel:5.2 Jun 20, 2016
@taylorotwell
Copy link
Member

I see thanks.

@sakalauskas
Copy link
Contributor Author

Thanks @taylorotwell !!!

@@ -1296,6 +1296,10 @@ protected function validateUnique($attribute, $value, $parameters)
if (strtolower($id) == 'null') {
$id = null;
}

if (filter_var($id, FILTER_VALIDATE_INT) !== false) {
$id = intval($id);
Copy link
Member

Choose a reason for hiding this comment

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

Should be just (int) $id. The two are the same, but this version is faster.

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.

4 participants