Laravel Shape Length

In the Laravel form input validation, you can specify the minimum and maximum length of the specified input field.

$inputRules = [ 'postCode' => 'min:3|max:8' ]; 

Result of checking

  • 'BA45TZ' = true
  • 'FF' = false
  • 'GH22 55XYZ' = false

However, if I do the same for the number, it will check if the input is less or more than the input, and then return it.

 $inputRules = [ 'cv2' => 'min:3|max:4' ]; 

Result of checking

  • '586' = false
  • '4' = true
  • '2' = false
  • '3' = true

I really want to check the length of numbers, not a numerical value. I can’t figure out how to do this. Google did not help, or I did not search for the right things.

Anyone have experience with this?

EDIT: I answered my question. I missed Laravels digits_between .

+5
source share
3 answers

As an idiot, I skipped the Laravels digits_between validator rule. I swear I looked through these rules, but we are here.

Thank you all for your help.

 $inputRules = [ 'cv2' => 'required|numeric|digits_between:3,4' ]; 
+12
source

This is the expected behavior of the Laravel size , min and max checks. You cannot change it.

For string data, the value corresponds to the number of characters. For numeric data, the value corresponds to a given integer value. For files, the size corresponds to the file size in kilobytes.

To solve this problem, you need to create a custom validation rule . Something like that:

 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 the same way:

 'cv2' => 'min_length:3|max_length:4' 
+6
source

For the length in the number you must use the size, but you cannot specify ranges in it. So, it’s best to do a regular check. 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.

0
source

Source: https://habr.com/ru/post/1213621/


All Articles