-
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
Added support for "true"
and "false"
text values to the Boolean validation rule for compatibility with FormData
#43955
Added support for "true"
and "false"
text values to the Boolean validation rule for compatibility with FormData
#43955
Conversation
"true"
and "false"
text values for compatibility with FormData
0e2d331
to
7c812ad
Compare
Please see previous attempts, for example: #40041 |
"true"
and "false"
text values for compatibility with FormData"true"
and "false"
text values to the Boolean validation rule for compatibility with FormData
@andrey-helldar This example just illustrates possible breaking changes. P.s: and no, its comparison for boolean value, not an empty string.
than you see the 'false'. The string is not empty |
@igorgaming, the developer must determine what goes into the database. From the frontend side, the string equivalent comes to the API. Yes, this is a shortcoming of the JS FormData technology. And if everything you say had a weight, the See: https://www.php.net/manual/en/filter.filters.validate.php |
@andrey-helldar So, are you suggesting that the developer should do an additional check on bool before the data puts into the database? Also see: https://www.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting |
public function __invoke(UpdateRequest $request)
{
$is_admin = $request->boolean('is_admin'); // boolean value `true` or `false`
// ...
}
|
I suggest just creating a new Rule::boolean method that creates a Boolean rule class which can accept additional options for values that should be allowed:
Feel free to send a PR for that if you wish. |
@andrey-helldar ofc in this case its ok, because |
Very often, data is sent from the web interface along with the file.
Unfortunately, you can only send a file through JS using
FormData
, and it has its own characteristics and limitations. For example, it can only transfer strings:At this point, we are faced with another problem - the validation of a boolean value:
Because "true" and "false" are strings of 4 and 5 characters respectively, not booleans.
For this reason, in the above request, the validator will return the message:
Of course, you can use the
filter_var
function when checking, but then another problem arises - it definesnull
values asfalse
:Therefore, it is better not to use it, because. often it is necessary to catch exactly
null
value.That is why I propose to extend the existing variable definition method by adding string values for
true
andfalse
to it.Since defining string values changes behavior, I consider these changes to be backwards incompatible.
At the moment, you have to insert a crutch in the form of a custom rule. In my opinion, this is wrong, given the presence of the rule in the "box".