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']);
Related
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 custom validation rule in my controller:
$this->validate($request, [
'currency' => [
'required',
'numeric',
'min:0',
'max:7'
],
'price' => [
'nullable',
"required_if:currency, !=, 0",
'numeric',
'min:1',
'max:1000000'
],
], $messages);
Why work in required_if:currency, ==, 0 and not work in this required_if:currency, !=, 0 case?
In my case price field required only when currency field value not equal to 0
I tired also:
required_unless,currency,0
required_unless:currency,0
I would suggest you use
required_unless:currency,0
according to [1]: https://laravel.com/docs/5.5/validation#rule-required-unless
required_if:currency, ==, 0 works because the currency value must be equal to any of the values that follow the value name (in this case currency). In other words, price is required in this case if currency is either == or 0.
So the == doesn't mean the usual "equals" in this case. It is just taken as a string value. That is also why required_if:currency, !=, 0 does not work as you expected it to.
To make the price field required only when the currency field value is not equal to 0, you could use required_unless:currency,0.
In other words, price is always required, unless currency is equal to 0.
You can use this:
Rule::requiredIf($request->get('currency') != 0)
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'];
I had a form in which a start date field is there. I need to check whether the start date entered is greater than Current date using yii validation rule.
Can any one help me in doing this?
This might work (needs to be added to the model rules)
array('startDate', 'compare', 'compareValue' => date("Y-m-d"), 'operator' => '>'),
$date_today = date('Y-m-d', strtotime(' -1 day'));
// -1, -2 depend on how back you want to go!!
$date_today = date("m/d/Y", strtotime($date_today));
return [
[
'alert_start_date', 'compare', 'compareValue' => $date_today,
'operator' => '>',
'message' => $attribute.'Alert Start Date Cannot be a date in the past'
],
];