-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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
[8.x] Conditional rules #38361
[8.x] Conditional rules #38361
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,56 @@ | ||||||
<?php | ||||||
|
||||||
namespace Illuminate\Validation; | ||||||
|
||||||
class ConditionalRules | ||||||
{ | ||||||
/** | ||||||
* The boolean condition indicating if the rules should be added to the attribute. | ||||||
* | ||||||
* @var callable|bool | ||||||
*/ | ||||||
protected $condition; | ||||||
|
||||||
/** | ||||||
* The rules to be added to the attribute. | ||||||
* | ||||||
* @var array | ||||||
*/ | ||||||
protected $rules; | ||||||
|
||||||
/** | ||||||
* Create a new conditional rules instance. | ||||||
* | ||||||
* @param callable|bool $condition | ||||||
* @param array|string $rules | ||||||
* @return void | ||||||
*/ | ||||||
public function __construct($condition, $rules) | ||||||
{ | ||||||
$this->condition = $condition; | ||||||
$this->rules = $rules; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Determine if the conditional rules should be added. | ||||||
* | ||||||
* @param array $data | ||||||
* @return bool | ||||||
*/ | ||||||
public function passes(array $data = []) | ||||||
{ | ||||||
return is_callable($this->condition) | ||||||
? call_user_func($this->condition, $data) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wrapping
Suggested change
Not sure if it's best to put it here or in the Validator on line 1085. Applied here, the setters would have no side-effects on the other conditionals. |
||||||
: $this->condition; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Get the rules. | ||||||
* | ||||||
* @return array | ||||||
*/ | ||||||
public function rules() | ||||||
{ | ||||||
return is_string($this->rules) ? explode('|', $this->rules) : $this->rules; | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Validation; | ||
|
||
use Illuminate\Validation\Rule; | ||
use Illuminate\Validation\ValidationRuleParser; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ValidationRuleParserTest extends TestCase | ||
{ | ||
public function test_conditional_rules_are_properly_expanded_and_filtered() | ||
{ | ||
$rules = ValidationRuleParser::filterConditionalRules([ | ||
'name' => Rule::when(true, ['required', 'min:2']), | ||
'email' => Rule::when(false, ['required', 'min:2']), | ||
'password' => Rule::when(true, 'required|min:2'), | ||
'username' => ['required', Rule::when(true, ['min:2'])], | ||
'address' => ['required', Rule::when(false, ['min:2'])], | ||
'city' => ['required', Rule::when(function (array $input) { | ||
return true; | ||
}, ['min:2'])], | ||
]); | ||
|
||
$this->assertEquals([ | ||
'name' => ['required', 'min:2'], | ||
'password' => ['required', 'min:2'], | ||
'username' => ['required', 'min:2'], | ||
'address' => ['required'], | ||
'city' => ['required', 'min:2'], | ||
], $rules); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be
array|string
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you send in a PR?