PHP code example of filippo-toso / validate-function

1. Go to this page and download the library: Download filippo-toso/validate-function 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/ */

    

filippo-toso / validate-function example snippets


use function FilippoToso\Validator\validate;
use App\User;

Route::get('/users/search', function() {

    // Call validate() passing the rules in the first parameter and the $successCallback as second.

    return validate([
        'name' => 'nullable|string',
        'email' => 'nullable|string',
        'orderBy' => ']);
        }

        $query->orderBy($data['orderBy']);

        // The return of this closure will be the validate() return value.

        return $query->get()->toArray();

    }, function ($validator) {

        // This is exactly what happens if you pass null as $failsCallback and
        // the Dingo framework is not installed.

        return response()->json([
            'message' =>'Faild validation.',
            'status_code' => 422,
            'errors' => $validator->errors(),
        ], 422);

    });

});