-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
Conversation
Great! This could clean up and simplify some code in FormRequests where if statements are put in the In terms of what it achieves, it's very similar to the Rule::when(function ($input) {
return $input->games >= 100;
}, 'required'); Can you nest multiple Rule::when($conditionA, [
'required',
Rule::when($conditionB, 'in:foo,bar')
]) |
Your closure use case seems pretty fine. Regarding nested conditions, it was hard for me to think of any use case where it was totally necessary, and it seems like it would add quite a bit of complication. Do you have any thoughts or use cases where having nested conditional rules is absolutely required? |
Looking at a few form requests of my own, I found a few rules inside nested ifs. Most of these are used to add rules for multiple fields (like a gate allowing something) as opposed to a single rule to an existing field, so they wouldn't benefit as much from supporting nested conditions. A I asked not because I need it, but I was curious about whether or not it would/should be possible. If not, I think it should be clearly documented that you cannot nest them so avoid confusion as it could give the impression to some that the rules used inside |
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Wrapping $data
in Illuminate\Support\Fluent
would make accessing the inputs slightly cleaner ($inputs->key
) through the __get
helper that checks if the property exists and otherwise return null, as opposed to needing to use Arr::get($inputs, 'key')
or $inputs['key'] ?? null
.
? call_user_func($this->condition, $data) | |
? call_user_func($this->condition, new Fluent($data)) |
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.
Thanks for this @taylorotwell, keen to take it for a spin. The number of times I've seen or had to write conditional blocks that break out of the validation |
❤️ Thanks Taylor! Got some refactoring to do on my project! |
/** | ||
* The rules to be added to the attribute. | ||
* | ||
* @var array |
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?
Looks like this PR changed how the validator returns data for keys that have no rules. Prior to this, keys without rules would still be included in the validated data. Now they are not. For example: // imagine this is the request that is posted
$request = [
'name' => 'Hello',
'description' => 'World'
];
$data = $this->validate([
'name' => 'required',
'description' => ''
]);
dd(array_keys($data));
// Before
['name', 'description']
// After
['name'] Will try to write a test for this, although I'm unsure at this point what needs to change. The cause seems to be the Edit: Also this seems like it may have been unintentional behavior how it worked before... but turns out we were relying on it as a way to trick a few extra fields into showing up in the validated data. Edit 2: Fixed by #38563 before I could make a PR! |
How can I access the value inside the callback, when field is an array.
I want to check if format only if value is not something like 24:00 But I can not access ther value of current openings to. |
The value should be on the request, so you should just be able to use |
Yes, but the only problem here is the field is an array. How can I know the array key on the fly?
|
Adds conditional rule support per @timacdonald's suggestion.