PHP code example of crhayes / validation

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

    

crhayes / validation example snippets


"    "crhayes/validation": "dev-master"
}

'providers' => array(
    // ...

    'Crhayes\Validation\ValidationServiceProvider'
)

'aliases' => array(
    // ...

    'GroupedValidator' => 'Crhayes\Validation\Facade'
)



namespace App\Services\Validators;

use Crhayes\Validation\ContextualValidator;

class UserValidator extends ContextualValidator
{
    protected $rules = [
        'first_name' => '

use App\Services\Validators\UserValidator;

...

$userValidator = new UserValidator(Input::all());
OR
$userValidator = UserValidator::make(Input::all());

...

if ($userValidator->passes()) {
  // yay, successful validation
}

// nay, get the errors
$errors = $userValidator->errors();



namespace App\Services\Validators;

use Crhayes\Validation\ContextualValidator;

class UserValidator extends ContextualValidator
{
    protected $rules = [
        'default' => [
            'first_name' => 'd a new rule for website while editing
        ]
    ];
}

use App\Services\Validators\UserValidator;

...

$userValidator = UserValidator::make(Input::all())->addContext('create');

if ($userValidator->passes()) {
  // yay, successful validation
}

// nay, get the errors
$errors = $userValidator->errors();

use App\Services\Validators\UserValidator;

$userValidator = UserValidator::make(Input::all())->addContext('edit');

if ($userValidator->passes()) {
  // yay, successful validation
}

// nay, get the errors
$errors = $userValidator->errors();

use App\Services\Validators\UserValidator;

$userValidator = UserValidator::make(Input::all())
    ->addContext(['create', 'profile']);

// or chained
$userValidator->addContext('create')->addContext('profile');



namespace App\Services\Validators;

use Crhayes\Validation\ContextualValidator;

class UserValidator extends ContextualValidator
{
    protected $rules = [
        'default' => [
            'first_name' => '

$userValidator = UserValidator::make(Input::all())
    ->addContext('edit')
    ->bindReplacement('email', ['id' => $currentUser->id]);

use App\Services\Validators\UserValidator;
use App\Services\Validators\CarValidator;
use Crhayes\Validation\GroupedValidator;

$userValidator = UserValidator::make(Input::only('first_name', 'last_name'));
$carValidator = CarValidator::make(Input::only('make', 'model'));

$groupedValidator = GroupedValidator::make()
    ->addValidator($userValidator)
    ->addValidator($carValidator);

if ($groupedValidator->passes()) {
  // yay, successful validation
}

// nay, get the errors
$errors = $groupedValidator->errors(); // return errors for all validators

$userValidator = UserValidator::make(Input::only('first_name', 'last_name'))
    ->addContext('create');
$carValidator = CarValidator::make(Input::only('make', 'model'))
    ->addContext('create');

$groupedValidator = GroupedValidator::make()
    ->addValidator($userValidator)
    ->addValidator($carValidator);

$userValidator = UserValidator::make(Input::only('first_name', 'last_name'));
$carValidator = Validator::make(Input::only('make', 'model'), [
    'make' => '



namespace App\Services\Validators;

use Crhayes\Validation\ContextualValidator;

class UserValidator extends ContextualValidator
{
    protected $rules = [
        'default' => [
            'first_name' => '    protected function addConditionalRules($validator)
    {
        $validator->sometimes('some_field', '