PHP code example of ygreis / laravel-validators

1. Go to this page and download the library: Download ygreis/laravel-validators 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/ */

    

ygreis / laravel-validators example snippets




namespace App\Validators;

use Ygreis\LaravelValidators\AbstractValidator;

class TestValidator extends AbstractValidator
{
    
    public function messages(): array
    {
        return [];
    }

    public function rules(): array
    {
        return [
            'name' => 'ther validators Here
        return [
            // TestTwoValidator::class
        ];
    }

}



public function TestValidatorException(Request $request)
{
    // Imagine that your Validator is in the path \App\Validators\TestValidator
    $validate = makeValidator(\App\Validators\TestValidator::class, $request->all());
    
    // Check has errors 
    if ($validate->errors()->count()) {
        dd('Get all errors', $validate->errors()->all());   
    }
}



public function TestValidatorException(Request $request)
{
    try {

        makeValidatorThrow(\App\Validators\TestValidator::class, $request->all());

        dd('All right!');

    // Exception from the library where it will be triggered when the Validator fails
    } catch (ValidatorException $exception) {
    
        // $exception->getMessage() Returns the first validation message that did not pass.
        // $exception->getErrors()->messages()->all() Returns all validation messages that did not pass.
        dd($exception->getMessage(), $exception->getErrors()->messages()->all());
    }
}




namespace App\Http\Requests;

use App\Validators\TestValidator;
use Ygreis\LaravelValidators\AbstractRequest;

class TestRequest extends AbstractRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'user.age' => 'nullable|integer|min:18|max:120',
        ];
    }

    /**
     * Get custom attributes for validator errors.
     *
     * @return array
     */
    public function attributes()
    {
        return [
            'user.age' => 'User Age',
        ];
    }

    public function validators()
    {
        return [TestValidator::class];
    }

}





namespace App\Http\Controllers;

use App\Http\Requests\TestRequest;

class TestController extends Controller {

    public function TestValidatorException(TestRequest $request)
    {
        dd('All Right');
    }

}



@if($errors->any())
    {!! implode('', $errors->all('<div>:message</div>')) !!}
@endif