I have the following rule in the validator:
'keywords' => 'array|required'
'date_intervals' => 'array|required'
The array needs to be populated with at minimum 1 element (should not be empty).
Is there an existing rule to achieve this, or is it required that I write a custom rule for it?
Does min:1 work with array validation?
Thanks.
No, you'd be better counting the array elements and then passing the count for validation.
$count = count(Input::get('keywords'));
$input = ['keywords' => Input::get('keywords'),'count' => $count];
$rules = ['keywords' => 'required|array', 'count' => 'min:1'];
Related
Say a model has a status property, that holds an array of strings. If this array contains a string name "Other", the status_other field should be required. I can achieve this with the following rules:
'status' => 'nullable|array',
'status_other' => Rule::requiredIf(in_array('Other', $this->model->status))
Is there a way to write the status_other rule as a string? I tried:
'status_other' => 'required_if:status,in:Other',
and
'status_other' => 'required_if:status,Other',
Both that did not work.
You can compare it by using '==' to match the string in the array.
'status_other' => 'required_if:status,==,Other',
help me to solve this
$this->validate($request, [
'period_starts' => 'required|numeric|digits:4|min:2010|max:'.(date('Y')),
'period_ends' => 'required|numeric|digits:4|gt:period_starts',
]);
Is there any way to validate period_ends greater than period_starts but only max +2 not allowed more
i think you need to add 2 two your gt:period_starts
and then use before_or_equal:new period_starts rule
reference
https://laravel.com/docs/8.x/validation#rule-before-or-equal
$this->validate($request, [
'period_starts' => 'required|numeric|digits:4|min:2010|max:'.(date('Y')),
'period_ends' => 'required|numeric|digits:4|gt:period_starts|max:'.(($request->period_starts)+2)
]);
I am trying to exclude a field from being validated when another field has a value (not null). In this case, i want 'deleted_pictures' to be excluded if product_pictures is an array (not null). My problem is that i think exclude_unless:product_pictures,null evaluates null as a string, and not as, well, null.
This is my rule.
ProductRequest.php
return [
'product_pictures.*' => 'required_with:product_pictures|file|max:2048|mimes:jpeg,jpg,png',
'product_pictures' => 'sometimes|array',
'deleted_pictures' => ['exclude_unless:product_pictures,null', 'required', new cant_delete_all($max,'1')],
];
read : exclude deleted_pictures unless product_pictures has a value of 'null'
I tried this to confirm my suspicion and it works like it should.
//test_field = 'some_value'
return [
'test_field' => 'required|string'
'product_pictures.*' => 'required_with:product_pictures|file|max:2048|mimes:jpeg,jpg,png',
'product_pictures' => 'sometimes|array',
'deleted_pictures' => ['exclude_unless:test_field,some_value', 'required', new cant_delete_all($max,'1')],
];
read : exclude deleted_pictures unless test_field has a value of 'some_value'
In my first case, deleted_pictures is excluded because it doesn't detect that product_pictures is 'null' (string)
While on the second case, deleted_pictures is NOT excluded because test_field matches the given value.
My question is, how do you evaluate null value in FormRequest Laravel?
So apparently you can just leave the second parameter blank to evaluate it as null
return [
'product_pictures.*' => 'required_with:product_pictures|file|max:2048|mimes:jpeg,jpg,png',
'product_pictures' => 'sometimes|array',
'deleted_pictures' => ['exclude_unless:product_pictures,', 'required', new cant_delete_all($max,'1')],
];
I'm not sure if this is how its supposed to be done or intended behavior. But im just gonna leave this answer just in case someone might need it. If someone can suggest the 'proper' way of doing it then I'll accept that instead.
I have to validate an integer which is not required or necessary but if it is entered, it must between 0 and 100
Actually I have to enter score value ranging 0 to 100
I tried 'digits_between:1,2' but value 0 and 100 cannot be entered
any help?
from Laravel 5.6 docs, validation rule between & rule nullable
$request->validate([
'field_name' => 'nullable|integer|between:0,100',
]);
if you want to accept float values as well, use rule numeric
$request->validate([
'field_name' => 'nullable|numeric|between:0,100',
]);
You can try this (docs).
[
'number' => 'sometimes|integer|between:0,100'
]
You can just use validation for condition without required
"field_name" => 'numeric|between:0,99.99',
This is the full line of code
$v = Validator::make($data, ['field' => 'numeric|between:0,99.99']);
I have a form that contains a select and an input field. To simplify the example, let's say the three options of the select are 1, 2, and 3.
I need to create a set of validation rules where input is only required if option 2 is selected. How can I create this rule?
Here is my set of rules so far:
$rules = array(
'selectField' => 'required',
'inputField' => '', // only required if option 2 is selected
);
You could use the required_if validation rule?
https://laravel.com/docs/5.1/validation#rule-required-if
$rules = [
'selectField' => 'required',
'inputField' => 'required_if:selectField,2',
];