PHP code example of a-h-abid / laravalerors

1. Go to this page and download the library: Download a-h-abid/laravalerors 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/ */

    

a-h-abid / laravalerors example snippets


use Illuminate\Validation\ValidationException;

// ...

    /**
     * @inheritDoc
     */
    protected function invalidJson($request, ValidationException $exception)
    {
        return response()->json([
            'message' => $exception->getMessage(),
            'errors' => $exception->errorsForApi(),
        ], $exception->status);
    }



namespace App\Rules;

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;

class InCategory implements ValidationRule
{
    /** @var string */
    public const RULE_NAME = 'in-category';

    /** @var array<int,string> */
    public const RULE_PARAMS = ['a', 'b', 'c'];

    /**
     * Run the validation rule.
     *
     * @param  \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString  $fail
     */
    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        if (!in_array($value, static::RULE_PARAMS)) {
            $fail('The :attribute you provided is not within the acceptable options.');
        }
    }
}