Validator::validate() and Validator::valid() not returning the same output #55736
Unanswered
bluesheep100
asked this question in
Q&A
Replies: 1 comment 7 replies
-
/**
* Returns the data which was valid.
*
* @return array
*/
public function valid()
{
if (! $this->messages) {
$this->passes();
}
return array_diff_key(
$this->data, $this->attributesThatHaveMessages()
);
} /**
* Run the validator's rules against its data.
*
* @return array
*
* @throws \Illuminate\Validation\ValidationException
*/
public function validate()
{
throw_if($this->fails(), $this->exception, $this);
return $this->validated();
} You can use validated() Example of FormRequest used without autowiring. |
Beta Was this translation helpful? Give feedback.
7 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Validator::validate()
andValidator::valid()
do not return the same list of valid data.Validator::validate()
returns only the data that has passed a validation rule.Validator::valid()
ALL data that did not explicitly fail a validation rule.Example:
Is this intentional? I can't seem to find anything about the fact that
Validator::valid()
returns unvalidated data in the docs.I discovered this difference in behaviour, because of an autosave feature, where it saves all of the fields to a JSON object (cast to/from a DTO-type class via a custom cast, but saved as JSON) and it needs to be able to save every valid field, regardless of how many are invalid. Currently I rely on some pretty sketchy workarounds to make it not save unvalidated data.
Beta Was this translation helpful? Give feedback.
All reactions