-
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
[9.x] Added has
and missing
methods to ValidatedInput
#42184
[9.x] Added has
and missing
methods to ValidatedInput
#42184
Conversation
Is the updated correct, Dries? |
Co-authored-by: Dries Vints <dries@vints.io>
I've also updated the other instances of array|mixed in the file for you 👍🏼 |
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.
Self review complete
has
and doesntHave
methods to ValidatedInput.has
and doesntHave
methods to ValidatedInput
I also wanted to add a method to the |
* @param mixed $keys | ||
* @return bool | ||
*/ | ||
public function doesntHave($keys) |
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.
I feel like missing
is more Laravel-ish
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.
I had the same thought, missing()
is used as the method name when interacting with a plain request()
instance.
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.
I'll get that updated 👍🏼
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.
I have changed the doesntHave
to missing
as I do agree it sounds more like Laravel's language, so I will update the description of the PR.
has
and doesntHave
methods to ValidatedInputhas
and missing
methods to ValidatedInput
Hey guys, I'm just thinking - does this PR add value to the framework? I could have sworn I recently had a use-case where I wanted to check if something was validated. Surely if the data was provided, it has to be validated. This does double check that it was provided AND valid, but not sure if that's really helping. Could this be sufficient, I think it may have been because I was conditionally applying the rule in my Edit; Also come to think of it, if people are using the validator outside of the FormRequest class, this would be very useful for people. |
@Sammyjo20 one thing I notice is the there could be a slight behavior difference between request->has and validatedInput->has... you use array_key_exists while request->has uses Arr::has under the hood. Thoughts? |
Please mark as ready for review after responding. Thanks. |
Ah yes, good spot. I have switched it to use From public function has($key)
{
$keys = is_array($key) ? $key : func_get_args();
$input = $this->all();
foreach ($keys as $value) {
if (! Arr::has($input, $value)) {
return false;
}
}
return true;
} |
May I ask why is Laravel not requiring a type-hinted return? For example, the public function has($key): bool;
public function missing($key): bool; Why I am saying so? I see other tests and author's tests are using public function has($keys)
{
return null; // Example
}
$this->assertEquals(false, $inputA->missing('name')); // This passes because false == null The fix would be to start using public function has($keys): bool;
$this->assertSame(false, $inputA->missing('name')); So, is there a why we are not doing so? I am asking so when I contribute in the near future I am already aware of this 👍 |
@matiaslauriti I was just keeping it consistent with the rest of the test. I assume it was written a while ago. I’d rather keep it consistent than make lots more changes to have to be reviewed. I agree though I prefer “assetTrue” and useful methods like that. |
We don't use return types right now in the codebase. We have no plans right now to add any. |
This PR introduces two new methods to the
Illuminate\Support\ValidatedInput
class.has($keys)
missing($keys)
These two methods are used to check the existence of validated input easily. For example, now with the
$request->safe()
method, I could now do:You can also provide an array, and it will validate each key...