PHP code example of andrey-vasin / laravel-request-filters

1. Go to this page and download the library: Download andrey-vasin/laravel-request-filters 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/ */

    

andrey-vasin / laravel-request-filters example snippets


public function rules():array {
    return [
        'email' => ['quired']
    ];
}

public function filters():array {
    return [
        'email' => [new FilterTrim, new FilterSanitizeEmail],
        'name' => [new FilterTrim, new FilterSanitizeText, new FilterCapitalize],
        'employees.*.name' => [new FilterCapitalize],
        'date' => [new FilterTrim, new FilterDate('d/m/Y')]
    ];
}

public function filters():array {
    return [
        'email' => 'trim|email',
        'name' => 'trim|sanitize|capitalize',
        'employees.*.name' => 'capitalize',
        'date' => 'trim|date:d/m/Y'
    ];
}

$input = $request->validated();
echo $input['email'];
//trimmed and sanitized email!

$validated_input = $request->validate([...]);

$filtered_result = InputFilter::filter(
                  $validated_input,
                  [
                  'email' => [new FilterTrim],
                  'name' => [new FilterTrim],
                  'meta.*.attributes' => [new MyCustom1Filter(1), new MyCustom2Filter(2)] 
                  ]
);

$validated_input = $request->validate([...]);

$custom_filters = [
    'custom1' => MyCustom1Filter::class,
    'custom2' => MyCustom2Filter::class
];

$filtered_result = InputFilter::filterFromString(
                  $validated_input,
                  [
                  'email' => 'trim',
                  'name' => 'trim',
                  'meta.*.attributes' => 'custom1:1|custom2:2'
                  ],
                  $custom_filters
);