Laravel: Validate min and max value based on input fields - php

I am trying to add validation for the min and max values. Both these values are coming from input fields.
I want to validate that max value (max_price) should always be greater than min value (min_price).
I am working on the Laravel 5.7
$validator = validator($request->all(),[
'min_price' => 'required|min:1"',
'max_price' => 'required|numeric|min:min_price',
]);

You can use the code below
$validator=validator($request->all(),[
'min_price'=>'required|min:1',
'max_price' => 'required|gt:min_price'
]);
References
https://laravel.com/docs/5.7/validation#rule-gt
https://laravel.com/docs/5.7/validation#rule-min

You can use the rule gt (Greather than) that expect the number of another field as first argument:
'min_price'=>'required|numeric|min:30',
'max_price'=>'required|numeric|gt:min_price'

Related

Laravel validator compare one value to another only if both are present

I'm using Laravel version v8.29.0
I am using the Laravel validator to validate values passed in from an HTTP request. In the request, I have an array of objects, each object has a min and a max value.
Right now I have this validation in place to make sure that the min value is less than the max value as well as that the max value is greater than the min value:
$this->validate($request, [
'someObject.*.someOtherObject.yetAnotherObject.min' => 'nullable|numeric|lte:someObject.*.someOtherObject.yetAnotherObject.max',
'someObject.*.someOtherObject.yetAnotherObject.max' => 'nullable|numeric|gte:someObject.*.someOtherObject.yetAnotherObject.min',
]);
This works just fine if both values are passed in, but for my application it is allowed for just one or the other or neither to be passed in.
If I only pass in the max value then I get an error message that says the max value needs to be greater than the min value even though the min value was not passed in.
Same thing for the min value, if I only pass in the min value then I get an error message that says the min value must be less than the max value.
My question is what syntax changes do I need to make to the code above to allow one or the other, neither or both values for min and max to be passed in and only validate the min and max in regards to each other if they are both passed in?
I don't think there's a built in way to conditionally use the lte and gte depending on if the other value is present.
What you could do is use the array syntax and a Closure instead:
$this->validate($request, [
'someObject.*.someOtherObject.yetAnotherObject.min' => [
'nullable',
'numeric',
function ($attribute, $value, $fail) use ($request) {
$max = $request->input(str_replace('min', 'max', $attribute));
if (isset($value, $max) && $value > $max) {
$fail(__('validation.lte.numeric', ['value' => $max]));
}
},
],
'someObject.*.someOtherObject.yetAnotherObject.max' => [
'nullable',
'numeric',
function ($attribute, $value, $fail) use ($request) {
$min = $request->input(str_replace('max', 'min', $attribute));
if (isset($value, $min) && $value < $min) {
$fail(__('validation.gte.numeric', ['value' => $min]));
}
},
],
]);
In the above, I'm also using __() (translate) helper function to get the default error message that Laravel would have returned.

Laravel validation 2 different values in a single field

I have a form that would has the field that inputs 2 different values in a single field.
First field is made up with select option dropdown. Once you have selected an option from the first field, the value from the first field will print on the input field. Then in the input field you need to add or type it manually another value to make it work. So the output would look like this:
Selected value - Value added manually
So my problem is how do I validate the field if each one of them is either empty.
I have no idea what I'm doing but this is what I've tried so far but it's not working.
'field' => 'required|in:' . implode(",", Model::values()) . '|unique:table'
This should be the expected output:
If the field value is Selected value - Value added manually, it is Valid.
If the field value is Selected value -, it should validate to input the 2nd value.
If the field value is - Value added manually, it should validate to input the 1st value.
I have searched on google but I'm not sure on what keywords should I searched on but my title sums up on what I have searched recently but I haven't found same problem as mine but if there is, please link it down thanks.
I would suggest using two separate inputs: A select for "selected value" and an input for "Value added manually". This will simplify the rules:
'field1' => 'required|in:' . implode(",", Model::values()),
'field2' => 'required',
But since you currently have a unique validation rule I suspect that's the reason you have it as a single field. In that case, you can create some custom validation rules.
Validate first value exists:
return false !== strpos($value, '-') && strlen(trim(explode('-', $value)[0])) > 0;
Validate first value is valid:
return in_array($value, Model::values());
Validate second value exists:
return false !== strpos($value, '-') && strlen(trim(explode('-', $value)[1])) > 0;
You can then add those rules to the chain:
'field' => ['required', new FirstValueExists, new FirstValueValid, new SecondValueExists, 'unique:table'],
The reason I have two rules for the first value is to allow for more specific error messages ("Value 1 missing" and "Value 1 invalid"). If you don't care about that you can combine them into a single rule.
Note that I haven't actually tested this but it should work.

Laravel required_if does not accept other parameters

I have some problems with Laravel's validation. I'm using required_if validator.
What I want to achieve: When the hilightColorBool is true the hilightColor should be required, should be integer between min:1 and max:5.
...
'hilightColorBool' => 'required|boolean',
'hilightColor' => 'required_if:hilightColorBool,1|integer|min:1|max:5'
...
But when I'm doing it like above it always returns that hilghtColor is bool, when I do remove the integer, min and max from hilightColor it works good, but I need to validate anyway if it is integer between 1 and 5. It seems weird to me.
Going on from my comment, i may do it like this:
$rules = [
'hilightColorBool' => 'required|boolean',
];
if (is_bool($request->get('hilightColorBool'))) {
$rules['hilightColor'] = 'required|integer|min:1|max:5';
}
then simply do:
$request->validate($rules);

Laravel validate int or float numbers as string

in my project. all mount maybe int or float or was even double.in database amount column type is varchar and for each choose amount by user i have simple limitation, for example amount must be more than some value and less that some value. but i get error validation in laravel.
$ps = DB::table('merchant_web_service')->whereCustomerKey($request->input('customer_key'))->first();
/* $ps->minimum_range_buy is 5 and $ps->maximum_range_buy is 10*/
$validate_amount = Validator::make($request->all(),
['amount' => "required|min:$ps->minimum_range_buy|max:$ps->maximum_range_buy"]);
validator error is:
"The amount must be at least 10 characters."
my test amount values: 1000,100.1
Since you didn't specify any rule for the input data type, it validates it as a string. Try numeric and between rules.
$validate_amount = Validator::make($request->all(),
['amount'=>
"required|numeric|between:$ps->minimum_range_buy,$ps->maximum_range_buy"
]);
try this
$rules = [
'your_field' => 'required|regex:/^\d*(\.\d{2})?$/'
]

Laravel rule validation for numbers

I have the following rules :
'Fno' => 'digits:10'
'Lno' => 'min:2|max5' // this seems invalid
But how to have the rule that
Fno should be a digit with minimum 2 digits to maximum 5 digits and
Lno should be a digit only with min 2 digits
If I correctly got what you want:
$rules = ['Fno' => 'digits_between:2,5', 'Lno' => 'numeric|min:2'];
or
$rules = ['Fno' => 'numeric|min:2|max:5', 'Lno' => 'numeric|min:2'];
For all the available rules: http://laravel.com/docs/4.2/validation#available-validation-rules
digits_between :min,max
The field under validation must have a length between the given min
and max.
numeric
The field under validation must have a numeric value.
max:value
The field under validation must be less than or equal to a maximum
value. Strings, numerics, and files are evaluated in the same fashion
as the size rule.
min:value
The field under validation must have a minimum value. Strings,
numerics, and files are evaluated in the same fashion as the size
rule.
$this->validate($request,[
'input_field_name'=>'digits_between:2,5',
]);
Try this it will be work
The complete code to write validation for min and max is below:
$request->validate([
'Fno' => 'integer|digits_between:2,5',
'Lno' => 'min:2',
]);
Laravel min and max validation do not work properly with a numeric rule validation. Instead of numeric, min and max, Laravel provided a rule digits_between.
$this->validate($request,[
'field_name'=>'digits_between:2,5',
]);
Also, there was just a typo in your original post.
'min:2|max5' should have been 'min:2|max:5'.
Notice the ":" for the "max" rule.
In addition to other answers, i have tried them but not worked properly. Below code works as you wish.
$validator = Validator::make($input, [
'oauthLevel' => ['required', 'integer', 'max:3', 'min:1']
]);

Categories