Replies: 2 comments 5 replies
-
Well, the validation rules for those scenarios aren't passing, they are just not run at all. See here https://laravel.com/docs/9.x/validation#implicit-rules |
Beta Was this translation helpful? Give feedback.
-
I agree that this behavior is not normal and have to change. I encountered this issue in all the companies for which I worked. Developers were not aware of this behavior and did not expect it to work this way. This behavior is described in the documentation, but it does not have enough visibility. From what I know, this was intended because in HTML forms, there is no difference between an unfilled input and one filled with an empty string. HTML forms send an empty string in both cases (for some input types), and so, there is no way in PHP to determine if the provided empty string was intentional or not. If Laravel were to trigger validation when an empty string is provided, it would mean that users would have to fill out every input field in the form in order to send it, and there would be no optional input fields. That being said, whether intentional or not, the input is still sent to the backend, and so, if an empty string is provided against the defined rules, the validator should not allow it. The case of an optional input field sent without XHR is a very specific case. Instead of having strange behaviors with the validator and/or the @ohtyap To answer your question, I typically use Laravel to develop APIs, and since I don't work with HTML forms, I use a composer patch to fix the validator. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I know there are already a few issues about exactly this topic - so I didn't wanted to open a new issue and preferred to open a discussion here.
Given the following validation rule and data:
Less surprising,
$validator->fails()
tells us that everything is fine with the data.But now take this example:
In this case, validation is passing => an empty string is a valid email address?
A few other examples that pass the validation:
Of course, with adding something like
sometimes|required
orfilled
these examples will work like expected. When looking at the code, I understand the situation on a technical level. But from a conceptional point of view, I expect that a given value is validated against the given rules without addingrequired
orfilled
.Moreover, adding these rules brings up another issue: Fields that contain
null
(even with thenullable
rule) stop working, same goes for an empty array. Butnull
and an empty array are valid values - like0
orfalse
.Requirement of a field: An optional field, which can either be null or a string with minimum 5 characters and maximum 255 character
As far as I understand, a lot of these behavior are coming from validating input coming from the request (with enabled middlewares like
TrimStrings
orConvertEmptyStringsToNull
). And the behavior might make sense in these situations. But there are other scenarios - like validating data coming from an XML or CSV, from any external resource like a remote API.It is possible to work around with all the things above - adding own (implicit) rules, not using the built-in
required
. But at the end, it is a work-arround for a very basic requirement of the validation system (and it should not be needed to add them yourself).If there is a validation rule called
email
, the given value should get validated against the email validator. This is currently not happening with an empty string. Same goes for all other rules -in
,min
,max
,date
etc. (all none implicit rules) - an empty string bypass all these rules (unlessrequired
is given too).Do you have similar experiences - or how dou you handle these things?
Beta Was this translation helpful? Give feedback.
All reactions