Get the solution ↓↓↓
If you want a minimal example here it is:
$validator = Validator::make([
'a' => null
], [
'a' => 'sometimes|integer'
]);
dump($validator->passes()); // false
$validator = Validator::make([
'a' => null
], [
'a' => 'nullable|integer'
]);
dump($validator->passes()); // true
Some notes: There's a middleware included in yourKernel.php by default calledConvertEmptyStringsToNull which when commented out will make data coming as empty from forms to be basically treated as missing instead of null.
sometimes|integer is functionally identical tointeger in this particular case becausea can either be missing, or an integer if it's not missing.
In generalsometimes is like a "guard", you apply the validation rules on the right side of the guard if the field is present but you do nothing when absent. Otherwise withoutsometimes the validation rules always run but all (except therequired* rules) pass when the field is missing.
Let's say your request contains:{name: 'value'}
I believe that:
sometimes means that thename fields can be present or not in the request body. Means it'll pass validation even if there is noname field on the requestnullable means that the field must be present, but the field value can benull. But in this case, you have to provide thename field even with a null value.Let's say you have form with a particular input that is displayed only depending on previous input values. So this field is not always available: you can usesometimes as its "normal" that the user won't always submit this field.
In the other hand, if the field is always displayed but not required: you'll allownull values, but not the fact that the field is missing in the body: as it may be a bot or anything else that changed your form: not correct.
Yes there is. When u have the nullable validation rule that means that the field that it is associated with it can have a null value when it is sent making it optional but it will always be validated.
'test_field1' => 'nullable|integer'
On the other hand the sometimes validation rule applies all other validation rules associated with a field only when that field is sent via the request. For example :
'test_field1' => 'sometimes|integer'
This means that test_field1 will bi validated if it is an integer only when the request data has that field.
You can read more about the validation rules in the official laravel documentation : https://laravel.com/docs/8.x/validation#conditionally-adding-rules https://laravel.com/docs/8.x/validation#rule-nullable
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Laravel is a free open source PHP framework that came out in 2011. Since then, it has been able to become the framework of choice for web developers. One of the main reasons for this is that Laravel makes it easier, faster, and safer to develop complex web applications than any other framework.
https://laravel.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.