Laravel validation value range between - laravel

I want my input value of range 9-12 and i am applying
'check' => 'required|digits_between:9,12'
digits_between calculate the length of digit.
but i don't want to validate character length. i want to validate only character value
$validatedData = $request->validate([
'check' => 'required|digits_between:9,12',
],
[
'check.digits_between'=> 'Value must be between 9 to 12',
]);

You are looking for the between[min,max] rule:
'integer|between:9,12'
// or
'numeric|between:9,12'
Adding integer or numeric to make sure that between that uses the same system as size treats this as a number.
Laravel 8.x Docs - Validation - Available Rules - between
Laravel 8.x Docs - Validation - Available Rules - integer
Laravel 8.x Docs - Validation - Available Rules - numeric

Related

Laravel Validation of Array

Laravel Help with validation
Hi everyone I want to validate an array of a foreign key to make sure it exists I am doing
'activities' => 'required|array',
'activities.*' => 'exists:admin_activities,id',
but it's skipping this validation
any solution for this
You can validate each element of an array in Laravel with "dot notation" to For example, to validate that each id in a given array input field is exists in admin_activities , you may do the following:
'activities.*.id' => 'required|exists:admin_activities,id',

How to add validation for field accept number and null? [Laravel 7.x]

I want to store the score value as a number when is_pass is true. If is_pass is false then score should be null. So I added validation like below for score
'score' => 'required_if:is_pass,true|numeric'
But I have an issue when is_pass is false then also it's expected score value as numeric. How to solve this issue? Is there any diffrent option is there other than custom validation? I am using laravel 7.
You should define a variable to keep you rules.
Then base on condition change your rules.
if((bool) $request->is_pass === true)
$rules['score'] = 'required_if:is_pass,true|numeric';
else
$rules['score'] = 'required_if:is_pass,false|null';
You can use nullable validation with numeric here and here
'score' => 'nullable|numeric'
what this will do is check if the value is not null then apply numeric validation otherwise skip validation and accept it as null.

Validate a phone number in Laravel

How would I go about validating a number in laravel.
I need the number stored in the following format 353861111111.
353 will be the prefix, and the user types in the rest. If the user types in 086, this is invalid.
You can use regex as:
'phone' => 'required|regex:/(353)[0-9]{9}/'
This checks for the pattern with starting with 353 followed by 9 digits having values from 0-9.
Or you can build a custom validator in boot method of AppServiceProvider.php:
Validator::extend('phone_number', function($attribute, $value, $parameters)
{
return substr($value, 0, 3) == '353';
});
This will allow you to use the phone_number validation rule anywhere in your application, so your form validation could be:
'phone' => 'required|numeric|phone_number|size:11'
In your validator extension you could also check if the $value is numeric and 11 characters long.
Here is how I did it on an older Laravel 4 project we have to update from time to time:
'phone' => 'required|regex:/^\d{3}-\d{3}-\d{4}$/',
Try this regex expression:
'number' => 'required|regex:^[3][5][3][\d]{8}[\d]$'

Laravel price validation only accept positive number and not only 0

I want to validate a "price" field in Laravel.
The price should only contain numbers without (.) or (,) and cant start with 0 as well.
Eg
2500 // Pass
02500 // Fails
12.12 // Fails
12,12 / Fails
The rule I have now looks like this:
'price' => 'required|integer|not_in:0',
It seems to work with the example above, however I dont understand it. Why does not integer allow something like 0123 with a starting zero. I just want to make sure that my rule works as excpected and that I dont miss something
Thanks
This works for me:
'price' => 'required|numeric|gt:0',
You can format the input before sending it to the validator in your Request class.
public function formatInput()
{
$input = array_map('trim', $this->all());
$input['price'] = (int)($input['price']);
$this->replace($input);
return $this->all();
}
Then you can use
'price' => 'required|integer|min:0',
Hope this helps
if you're not sure about the rule "integer", you can use the regex expression validation as following :
'price' => 'required|regex:^[1-9][0-9]+|not_in:0',
I had some issue same way as you, and since then i always used the regex validation for this kind of requirements. Furthermore it allow you to take back the same regex if you want to make a front validation with js.
Here is the laravel function allowing to validate integers :
public function validateInteger($attribute, $value)
{
return filter_var($value, FILTER_VALIDATE_INT) !== false;
}
SO it's PHP itself that is concerned by this behavior. PHP tells us this thing about FILTER_VALIDATE_INT :
Validates value as integer, optionally from the specified range, and
converts to int on success.
No other informations are set by PHP about the "0" value, but it's known that this function doesn't consider numbers starting by "0".
Use regex rule to not allow for integer with starting 0
'price' => 'required|integer|not_in:0|regex:^[1-9][0-9]+',

Laravel 5.4 different value validation rule in array

I have an input with an array of entities with an ID which must be unique
I've tried this:
'authors.*.id' => 'different:authors.*.id'
But it says 'The authors.0.id and authors.0.id must be different'
So what is a right way to validate this?
You want to use distinct rule.
When working with arrays, the field under validation must not have any duplicate values.
'foo.*.id' => 'distinct'

Resources