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})?$/'
]
Related
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'
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);
I am trying to validate a phone number. First I strip the '-' out then I convert the inputed string to an integer. When I try to validate the integer I get an error that the input must be an integer. However, I just converted the string to and integer. Why am I getting this error and how do I fix it?
Here is my code
$request->val = str_replace('-', '', $request->val);
Log::debug($request->val);
Log::debug(gettype($request->val)); //outputs string
$request->val = intval($request->val);
Log::debug(gettype($request->val));//outputs integer
$this->validate($request, [
'val' => 'Integer|min:10|max:15'//Get an error must be interget
]);
Use ->merge to alter the value of a request attribute, do not attempt to mutate it directly, as the state will not be saved:
$request->merge(['val', intval(str_replace('-', '', $request->val))]);
In Laravel form input validation, you can specify a minimum and a maximum length of a given input field.
$inputRules = [
'postCode' => 'min:3|max:8'
];
Validation outcome
'BA45TZ' = true
'FF' = false
'GH22 55XYZ' = false
However if I do the same for a number, it will validate whether the input is less than or greater than the input, then return on that.
$inputRules = [
'cv2' => 'min:3|max:4'
];
Validation outcome
'586' = false
'4' = true
'2' = false
'3' = true
I actually want to validate a numbers length not it's numerical value. I can't figure out how to do this. Google has been no help, or I am not searching the right thing.
Anybody had any experience with this?
EDIT: I have answered my own question. I had missed Laravels digits_between.
Like an idiot, I missed Laravels digits_between validator rule. I swear I had scoured those rules, but here we are.
Thank you everyone for your help.
$inputRules = [
'cv2' => 'required|numeric|digits_between:3,4'
];
That's the expected behavior of Laravel's size, min and max validations. You can't change it.
For string data, value corresponds to the number of characters. For numeric data, value corresponds to a given integer value. For files, size corresponds to the file size in kilobytes.
To solve this you have to create a custom validation rule.
Something like this:
Validator::extend('min_length', function($attribute, $value, $parameters){
return strlen($value) >= $parameters[0];
});
Validator::extend('max_length', function($attribute, $value, $parameters){
return strlen($value) <= $parameters[0];
});
And you can use them in the same way:
'cv2' => 'min_length:3|max_length:4'
For length in number you've to use size, but you can't put ranges in it. So, the best thing to do is making a custom validation.
Here is the documentation in laravel: http://laravel.com/docs/5.0/validation#custom-validation-rules
And an example:
Validator::extend('minsize', function($attribute, $value, $parameters)
{
return strlen($value) >= $parameters[0];
});
And do the same with maxsize.
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']
]);