PHP code example of krazydanny / laravel-validator

1. Go to this page and download the library: Download krazydanny/laravel-validator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

krazydanny / laravel-validator example snippets


$app->register( KrazyDanny\Laravel\Validation\ServiceProvider::class );


$myValidator = Validator::make(
	$values,
	[
		'lat' => 'latitude',
		'lng' => 'longitude',
		'mac' => 'mac_address',
	],
	$messages
);

$myValidator = Validator::make(
	$values,
	[
		'attributes' => 'array_of_regex:/(some_regex)/',
	],
	[
		'attributes.array_of_regex' => 'Some message',
	]
);

[ 'value A', 'value B', 'value C' ]
[]


'value A, value B, value C'
"[ 'value A', 'value B', 'value C' ]"
"[]"


$myValidator = Validator::make(
	$values,
	[
		'active' => 'boolean_strict',
	],
	[
		'active.boolean_strict' => 'Some message',
	]
);

true
false


1
0
'true'
'false'

$myValidator = Validator::make(
	$values,
	[
		'var_name' => 'camel_case',
	],
	[
		'var_name.camel_case' => 'Some message',
	]
);

'camel',
'camelCase'
'camelCaseN'
'camelCaseNotation'


'Camel',
'CamelCase',
'camelcase',
'NCamelCase'


$myValidator = Validator::make(
	$values,
	[
		'color'  => 'color_hex',
		'colors' => 'color_hex', // also works with an array of values
	],
	[
		'color.color_hex' => 'Some message',
	]
);

'#FFFFFF'
'#FF3333'
'#ffffff'
'#5AD66A'


'FFFFFF'
'#FF33'
'#FF3333A'
'#'


$myValidator = Validator::make(
	$values,
	[
		'start_at' => 'date_gt_min:2020-03-01 00:00:00,86400',
	],
	[
		'start_at.date_gt_min' => 'Some message',
	]
);

$myValidator = Validator::make(
	$values,
	[
		'start_at' => 'date_gt_max:2020-03-01 00:00:00,86400',
	],
	[
		'start_at.date_gt_max' => 'Some message',
	]
);

$myValidator = Validator::make(
	$values,
	[
		'start_at' => 'date_lt_min:2020-03-01 00:00:00,86400',
	],
	[
		'start_at.date_lt_min' => 'Some message',
	]
);

$myValidator = Validator::make(
	$values,
	[
		'start_at' => 'date_lt_max:2020-03-01 00:00:00,86400',
	],
	[
		'start_at.date_lt_max' => 'Some message',
	]
);

$myValidator = Validator::make(
	$values,
	[
		'id_doc'       => 'document_number',
		'license_num'  => 'document_number',
		'passport_num' => 'document_number',
	],
	[
		'passport_num.document_number' => 'Some message',
	]
);

'123123123123'
'abcabcabcabc'
'a1b2c3d4e5f6'
'20-30764053-0'
'20/30764053/0'
'20 30764053 0'
'20/3076 405-30'
'20/3076.405-30'
'a1-b2c3d4e5f-6'


'20--30764053-0'
'20//30764053/0'
'20  30764053 0'
'20/3076..405-30'
'N° 23123123312'
'n° 23123123312'
'n°23123123312'


$myValidator = Validator::make(
	$values,
	[
		'price' => 'float',
		'rate'  => 'float:strict',
	],
	[
		'price.float' => 'Some message',
		'rate.float'  => 'Some message',
	]
);

0
100
0.00
0.01
100.00
100.01
'0'
'100'
'0.00'
'0.01'
'100.00'
'100.01'


'0,00'
'100,00'


$myValidator = Validator::make(
	$values,
	[
		'distance' => 'geo_distance',
		'radius'   => 'geo_distance',
	],
	[
		'distance.geo_distance' => 'Some message',
		'radius.geo_distance'   => 'Some message',
	]
);

0
100
1000
'0'
'1m'
'100km'


-1
'-1m'
'100k'


$myValidator = Validator::make(
	$values,
	[
		'category' => 'kebab_case',
	],
	[
		'category.kebab_case' => 'Some message',
	]
);

'kebabcase',
'kebab-case'
'kebab-case-notation'


'kebabCase',
'kebab-Case',
'Kebab-case',


$myValidator = Validator::make(
	$values,
	[
		'lat' => 'latitude',
	],
	[
		'lat.latitude' => 'Some message',
	]
);

1
90
-90
1.00,
30.010203
-67.50685
90.000000
-80.9999900000999


91
-91
-100
180.00
90.00000000000001
-90.00000000000001

$myValidator = Validator::make(
	$values,
	[
		'lng' => 'longitude',
	],
	[
		'lng.longitude' => 'Some message',
	]
);

1
-180
180.00,
30.010203
-67.50685
90.000000
-179.9999900000999


181
-181
188.99
200.000
180.00000000000001
-180.00000000000001


$myValidator = Validator::make(
	$values,
	[
		'mac_addr' => 'mac_address',
	],
	[
		'mac_addr.mac_address' => 'Some message',
	]
);

'00:00:00:00:00:00'
'EE:EE:EE:EE:EE:EE'
'A1:01:A2:02:A3:03'


000000000000
'A1:01:A2:02'
'A1.01.A2.02.A3.03'
'A1 01 A2 02 A3 03'
'A1:01:A2:02:A3:03:'


$myValidator = Validator::make(
	$values,
	[
		'attributes' => 'object',
	],
	[
		'attributes.object' => 'Some message',
	]
);

[ 'fieldA' => 'value A', 'fieldB' => "value B" ]
new stdClass
(object)[ 'field' => 'value' ]


[ 'value A', 'value B' ]
"{}"


$myValidator = Validator::make(
	$values,
	[
		'class_name' => 'pascal_case',
	],
	[
		'class_name.pascal_case' => 'Some message',
	]
);

'Pascal'
'Pascalcase'
'PascalCase'
'PascalCaseN'
'PascalCaseNotation'


'pascal'
'pascalCase'
'pascalcase'
'nPascalCase'


$myValidator = Validator::make(
	$values,
	[
		'param_name' => 'snake_case',
	],
	[
		'param_name.snake_case' => 'Some message',
	]
);

'snakecase'
'snake_case'
'snake_case_n'
'n_snake_case'
'snake_case_notation'