PHP code example of laravel-validators / foundation

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

    

laravel-validators / foundation example snippets




namespace App\Providers;

use LaravelValidators\Foundation\ValidationServiceProvider as ServiceProvider;

class ValidationServiceProvider extends ServiceProvider
{
  /**
   * The validation rules provided by your application.
   *
   * @var array
   */
  protected $rules = [
  //  'gender' => \App\Validators\GenderValidator::class,
  ];

  /**
   * Register the Closure based validators for the application.
   *
   * @return void
   */
  public function rules()
  {
    //
  }
}

App\Providers\ValidationServiceProvider::class

Validator::extend('gender', App\Validators\GenderValidator::class, 'Must be male or female.');



namespace App\Validators;

class GenderValidator
{
  /**
   * Validate the given value.
   *
   * @param  string  $attribute
   * @param  mixed  $value
   * @param  array  $parameters
   * @param  \Illuminate\Contracts\Validation\Validator  $validator
   * @return  bool
   */
  public function validate($attribute, $value, $parameters, $validator)
  {
    return in_array($value, ['male', 'female']);
  }
}

protected $rules = [
  'gender' => \App\Validators\GenderValidator::class,
];

/**
 * Register the Closure based validators for the application
 *
 * @return void
 */
public function rules()
{
  $this->rule('gender', function ($attribute, $value, $parameters, $validator) {
    return in_array($value, ['male', 'female']);
  });
}



namespace App\Validators;

class GenderValidator
{
  /**
   * Validate the given value.
   *
   * @param  string  $attribute
   * @param  mixed  $value
   * @param  array  $parameters
   * @param  \Illuminate\Contracts\Validation\Validator  $validator
   * @return  bool
   */
  public function validate($attribute, $value, $parameters, $validator)
  {
    return in_array($value, ['male', 'female']);
  }
  
  public function sanitize($value)
  {
    return strtolower($value);
  }

  /**
   * Set the validation error message.
   *
   * @return string
   */
  public static function message()
  {
    return 'You can only specify male or female as your gender.';
  }
}



namespace App\Validators;

class GenderValidator
{
  /**
   * Validate the given value.
   *
   * @param  string  $attribute
   * @param  mixed  $value
   * @param  array  $parameters
   * @param  \Illuminate\Contracts\Validation\Validator  $validator
   * @return  bool
   */
  public function validate($attribute, $value, $parameters, $validator)
  {
    return in_array($value, ['male', 'female']);
  }
  
  /**
   * Sanitize the given value before it is validated.
   *
   * @param mixed $value
   * @return mixed
   */
  public function sanitize($value)
  {
    return strtolower($value);
  }
}
bash
$ php artisan make:validator GenderValidator