PHP code example of nestecha / laravel-json-api-validation

1. Go to this page and download the library: Download nestecha/laravel-json-api-validation 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/ */

    

nestecha / laravel-json-api-validation example snippets

 php
/**
 * Render an exception into an HTTP response.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Exception  $exception
 * @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse
 */
public function render($request, Exception $exception)
{
    if ($exception instanceof \Nestecha\LaravelJsonApiValidation\Exception\JsonApiValidationException) {
        $responseFactory = new \Nestecha\LaravelJsonApiValidation\ResponseFactory();
        return $responseFactory->fromErrors($exception->errors()->getArrayCopy());
    }

    return parent::render($request, $exception);
}
 php
public function home(Request $request)
{
    $validator = new \Nestecha\LaravelJsonApiValidation\JsonApiValidator();
    $validator->validateAsJsonApi($request->all(), ['title' => '
bash
php artisan vendor:publish --tag=config
 php
$app->configure('json-api-validation');
 php
public function home(Request $request)
{
    $validator = new JsonApiValidator('name-of-your-config-file');
    $validator->validateAsJsonApi($request->all(), ['title' => '
 php
class UppercaseRule implements Rule
{
    /**
     * @inheritDoc
     */
    public function passes($attribute, $value)
    {
        return strtoupper($value) === $value;
    }

    /**
     * @inheritDoc
     */
    public function message()
    {
        return "The :attribute should be uppercase.";
    }
}
 php
return [
    'uppercase-rule' => ['code' => 'VALIDATION_ERROR_UPPERCASE'],
];