PHP code example of laratalks / validator

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

    

laratalks / validator example snippets



#UserValidator.php

namespace YourApp\Validators;

use Laratalks\Validator\AbstractValidator;

class UserValidator extends  AbstractValidator
{

    protected $registrationRules = [
        'name' => [''min:64']
    ];

    protected $anotherScenarioRules = [
        'key1' => ['rule1', 'rule2'],
        'key2' => ['rule1', 'rule2']
    ];
    
}


# UserController.php

namespace Laratalks\Validator;

use YourApp\Validators\UserValidator;
use Laratalks\Validator\Exceptions\ValidationException;

class UserController extends Controller
{
    public function register(Request $request, UserValidator $valdiator)
    {
        try {
            // validate user input
            $valdiator
                ->setScenario('registration')
                ->validate($request->all());
            
        } catch (ValidationException $e) {
            // catch errors
            return $e->getErrors();
        }
    }
}