How to allow empty value for Laravel numeric validation - laravel

How to set not require numeric validation for Laravel5.2? I just used this Code but when i don't send value or select box haven't selected item I have error the val field most be numeric... I need if request hasn't bed input leave bed alone. leave bed validate ...
$this->validate($request, [
'provinces_id' => 'required|numeric',
'type' => 'required',
'bed' => 'numeric',
]);

If I understood you correctly, you're looking for sometimes rule:
'bed' => 'sometimes|numeric',
In some situations, you may wish to run validation checks against a field only if that field is present in the input array. To quickly accomplish this, add the sometimes rule to your rule list

In Laravel 6 or 5.8, you should use nullable. But sometimes keyword doesn't work on that versions.

Use sometimes instead of required in validation rules. It checks if only there is a value. Otherwise it treats parameter as optional.

You may need nullable – sometimes and
present didn't work for me when combined with integer|min:0 on a standard text input type - the integer error was always triggered.
A Note on Optional Fields
By default, Laravel includes the TrimStrings and ConvertEmptyStringsToNull middleware in your application's global middleware stack. These middleware are listed in the stack by the App\Http\Kernel class. Because of this, you will often need to mark your "optional" request fields as nullable if you do not want the validator to consider null values as invalid.
Tested with Laravel 6.0-dev
Full list of available rules

In laravel 5.5 or versions after it, we begin to use nullable instead of sometimes.

according to laravel documentation 8 you must to set nullable rule
for example:
$validated = $request->validate([
'firstName' => ['required','max:255'],
'lastName' => ['required','max:255'],
'branches' => ['required'],
'services' => ['required' , 'json'],
'contract' => ['required' , 'max:255'],
'FixSalary' => ['nullable','numeric' , 'max:90000000'],
'Percent' => ['nullable','numeric' , 'max:100'],
]);
in your case :
$this->validate($request, [
'provinces_id' => 'required|numeric',
'type' => 'required',
'bed' => 'nullable|numeric',
]);

Related

Laravel - request validation for two nullable fields requiring at least one of them to be not-null

My model has two nullable fields, but at least one of them must be not-null.
How do I setup request validation for this ?
$request->validate([
'field_1' => ['nullable'],
'field_2' => ['nullable'],
...
]);
thanks
If you have only two filed comparisons then you can use required_without:foo,bar,...
$request->validate([
'field_1' => 'required_without:field_2',
'field_2' => 'required_without:field_1'
]);
if it has more than two fields then required_without_all:foo,bar,...
$request->validate([
'field_1' => 'required_without_all:field_2,field_3',
'field_2' => 'required_without_all:field_1,field_3',
'field_3' => 'required_without_all:field_1,field_2',
]);
As per Doc
required_without:foo,bar,...
The field under validation must be present and not empty only when any
of the other specified fields are empty or not present.
required_without_all:foo,bar,...
The field under validation must be present and not empty only when all
of the other specified fields are empty or not present.
Validation

How to validate laravel input only when data is present?

So I have a Laravel form where I'm collecting data, I use same form for creating and updating the table. Problem is that I only want the input fields to be validated when they contain data, this is my validation logic.
$validated = $request->validate([
'site_name' => 'sometimes|string',
'site_email' => 'sometimes|email',
'site_keywords' => 'sometimes|string',
'site_description' => 'sometimes|string',
'site_acronym' => 'sometimes|string|max:7',
'site_favicon' => 'sometimes|mimes:jpg,png,jpeg,svg',
'site_logo' => 'sometimes||mimes:jpg,png,jpeg,svg',
'site_currency' => 'sometimes|string',
'site_currency_symbol' => 'sometimes|string',
]);
But when the form is submitted, laravel returns an error for empty input fields, e.g it returns an error like so "The site name must be a string." for the first validation rule.
What am I doing wrong please?
EDIT
So I took the advice from #roj-vroemen and modified my code by using the nullable validation rule and it works just fine now.
Here's the new code block
$validated = $request->validate([
'site_name' => 'string|nullable|sometimes',
'site_email' => 'email|nullable|sometimes',
'site_keywords' => 'string|nullable|sometimes',
'site_description' => 'string|nullable|sometimes',
'site_acronym' => 'string|nullable|sometimes|max:7',
'site_favicon' => 'mimes:jpg,png,jpeg,svg|nullable|sometimes',
'site_logo' => 'mimes:jpg,png,jpeg,svg|nullable|sometimes',
'site_currency' => 'string|nullable|sometimes',
'site_currency_symbol' => 'string|nullable|sometimes',
]);
You'll want to use nullable in this case. sometimes basically means it only runs the validation for that field when it's present in the data.
In short:
Use nullable when you want to allow null values.
https://laravel.com/docs/8.x/validation#rule-nullable
Use sometimes if your only want to validate the field when it's present in the given input array.
https://laravel.com/docs/8.x/validation#validating-when-present
Note: you might want to make sure to filter out the empty values as the fields containing null will still be there:
$validated = array_filter($validated);

Handling CodeIgniter form validation (rule keys and data types)

Okay, so I've been searching for a while this question, but couldn't find an answer (or at least some direct one) that explains this to me.
I've been using CodeIgniter 3.x Form Validation library, so I have some data like this:
// Just example data
$input_data = [
'id' => 1,
'logged_in' => TRUE,
'username' => 'alejandroivan'
];
Then, when I want to validate it, I use:
$this->form_validation->set_data($input_data);
$this->form_validation->set_rules([
[
'field' => 'id',
'label' => 'The ID to work on',
'rules' => 'required|min_length[1]|is_natural_no_zero'
],
[
'field' => 'username',
'label' => 'The username',
'rules' => 'required|min_length[1]|alpha_numeric|strtolower'
],
[
'field' => 'logged_in',
'label' => 'The login status of the user',
'rules' => 'required|in_list[0,1]'
]
]);
if ( $this->form_validation->run() === FALSE ) { /* failed */ }
So I have some questions here:
Is the label key really necessary? I'm not using the Form Validation auto-generated error messages in any way, I just want to know if the data passed validation or not. Will something else fail if I just omit it? As this will be a JSON API, I don't really want to print the description of the field, just a static error that I have already defined.
In the username field of my example, will the required rule check length? In other words, is min_length optional in this case? The same question for alpha_numeric... is the empty string considered alpha numeric?
In the logged_in field (which is boolean), how do I check for TRUE or FALSE? Would in_list[0,1] be sufficient? Should I include required too? Is there something like is_boolean?
Thank you in advance.
The "label" key is necessary, but it can be empty.
The "required" rule does not check length, nor does the "alpha_numeric". It checks that a value is present, it does not check the length of said value. For that, there is min_length[] and max_length[].
If you're only passing a 0 or 1, then this is probably the easiest and shortest route.

Laravel Validation custom messages using nested attributes

I would like to ask for some advices about Laravel Validation...
Let's say I've got an input named invoiceAddress[name] and in a controller I've got a rule
$rule = ['invoiceAddress.name' => 'required',];
or just a
$validator = \Validator::make($request->all(), [
'invoiceAddress.name' => 'required',
]);
now, inside custom validation language file validation.php, am I able to nest attributes somehow? like:
'required' => ':attribute is mandatory',
'attributes' => [
'invoiceAddress' => [
'name' => 'blahblah'
],
],
If I try to nest the attribute the way above, i get
ErrorException
mb_strtoupper() expects parameter 1 to be string, array given
because I am using a field (as above)
['name' => 'blahblah']
I am trying to get custom messages using the file and the :attribute directive (as mentioned in the code above).
I am basically trying to do this How to set custom attribute labels for nested inputs in Laravel but i get the mentioned error...
Thank you in advance...
A Note On Nested Attributes
If your HTTP request contains "nested" parameters, you may specify them in your validation rules using "dot" syntax:
$this->validate($request, [
'title' => 'required|unique:posts|max:255',
'author.name' => 'required',
'author.description' => 'required',
]);
Reference: https://laravel.com/docs/5.3/validation#quick-writing-the-validation-logic (A Note On Nested Attributes Section)

Laravel 5.2 - validate value in array

I am trying to ensure a field is valid if the value appears in a predefined array, but it's not working for me.
The validation rule I am using is:
'title' => [
'required',
'in' => ['Mr', 'Mrs', 'Miss', 'Ms'],
],
But it seems to pass validation if I enter an invalid value, such as "Dr".
Anyone know the correct way to do this?
Try with a string validation rule instead of array:
'title' => 'required|in:Mr,Mrs,Miss,Ms';

Resources