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] Allow to pass collection to validation rules #23875

Merged
merged 1 commit into from
Apr 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/Illuminate/Validation/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Validation;

use Illuminate\Support\Collection;
use Illuminate\Support\Traits\Macroable;

class Rule
Expand Down Expand Up @@ -34,22 +35,30 @@ public static function exists($table, $column = 'NULL')
/**
* Get an in constraint builder instance.
*
* @param array|string $values
* @param array|string|\Illuminate\Support\Collection $values
* @return \Illuminate\Validation\Rules\In
*/
public static function in($values)
{
if ($values instanceof Collection) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe this should be generalized to Illuminate\Contracts\Support\Arrayable that Collection implements?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We'd need to rewrite In and NotIn rules to accept array and collections then.

Copy link
Contributor

Choose a reason for hiding this comment

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

Since Arrayable contains the toArray method all you would need to change was the condition I believe.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh I see you point now. Sorry guys.

I'd argue that Arrayable is implemented by following classes: Request, LengthAwarePaginator, MessageBag, Fluent etc.

Still think that Collection & array are the way to go.

$values = $values->toArray();
}

return new Rules\In(is_array($values) ? $values : func_get_args());
}

/**
* Get a not_in constraint builder instance.
*
* @param array|string $values
* @param array|string|\Illuminate\Support\Collection $values
* @return \Illuminate\Validation\Rules\NotIn
*/
public static function notIn($values)
{
if ($values instanceof Collection) {
$values = $values->toArray();
}

return new Rules\NotIn(is_array($values) ? $values : func_get_args());
}

Expand Down
4 changes: 4 additions & 0 deletions tests/Validation/ValidationInRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public function testItCorrectlyFormatsAStringVersionOfTheRule()

$this->assertEquals('in:"1","2","3","4"', (string) $rule);

$rule = Rule::in(collect([1, 2, 3, 4]));

$this->assertEquals('in:"1","2","3","4"', (string) $rule);

$rule = Rule::in('1', '2', '3', '4');

$this->assertEquals('in:"1","2","3","4"', (string) $rule);
Expand Down
4 changes: 4 additions & 0 deletions tests/Validation/ValidationNotInRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public function testItCorrectlyFormatsAStringVersionOfTheRule()

$this->assertEquals('not_in:"1","2","3","4"', (string) $rule);

$rule = Rule::notIn(collect([1, 2, 3, 4]));

$this->assertEquals('not_in:"1","2","3","4"', (string) $rule);

$rule = Rule::notIn('1', '2', '3', '4');

$this->assertEquals('not_in:"1","2","3","4"', (string) $rule);
Expand Down