Skip to content

[5.8] Fix database rules with WHERE clauses#28748

Merged
taylorotwell merged 1 commit intolaravel:5.8from
staudenmeir:database-rule
Jun 6, 2019
Merged

[5.8] Fix database rules with WHERE clauses#28748
taylorotwell merged 1 commit intolaravel:5.8from
staudenmeir:database-rule

Conversation

@staudenmeir
Copy link
Copy Markdown
Contributor

@staudenmeir staudenmeir commented Jun 6, 2019

exists and unique rules fail if a WHERE clause has an empty string value:

Validator::make(
    ['id' => 1],
    ['id' => Rule::exists('users')->where('name', '')]
)->passes();

This throws an exception:

Undefined offset: 1

The error is caused by rtrim(..., , ',') in Exists::__toString():

dump((string) Rule::exists('users')->where('name', ''));

// expected: exists:users,NULL,name,
// actual:   exists:users,NULL,name

The underlying issue is that ValidationRuleParser::parseParameters() parses parameters as CSV, but only receives plain text. This also affects values that can be interpreted as CSV:

Rule::exists('users')->where('name', '"foo"') // where "name" = 'foo'

We can fix it by converting all values to CSV as we do with in and notin rules (#21012).

Fixes #28697.


Theoretically, this can be a breaking change if someone works around the issue by providing CSV values:

 Rule::exists('users')->where('name', '""')

Do we consider cases like this?

@taylorotwell taylorotwell merged commit f76d0e2 into laravel:5.8 Jun 6, 2019
@staudenmeir staudenmeir deleted the database-rule branch June 6, 2019 14:08
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.

getExtraConditions Validation Undefined Offset: 1 Fault

2 participants