extend validator rules at ValidatorExtendServiceProvider

This commit is contained in:
printempw 2016-09-10 19:06:54 +08:00
parent a172eb0def
commit 9ef9137688
4 changed files with 69 additions and 33 deletions

View File

@ -15,35 +15,7 @@ class AppServiceProvider extends ServiceProvider
*/
public function boot()
{
View::addExtension('tpl', 'blade');
Validator::extend('username', function($attribute, $value, $parameters, $validator) {
return preg_match("/^([A-Za-z0-9\x{4e00}-\x{9fa5}_]+)$/u", $value);
});
Validator::extend('nickname', function($attribute, $value, $parameters, $validator) {
return $value == addslashes(trim($value));
});
Validator::extend('no_special_chars', function($attribute, $value, $parameters, $validator) {
return $value == addslashes(trim($value));
});
Validator::extend('playername', function($attribute, $value, $parameters, $validator) {
return preg_match("/^([A-Za-z0-9_]+)$/", $value);
});
Validator::extend('pname_chinese', function($attribute, $value, $parameters, $validator) {
return preg_match("/^([A-Za-z0-9\x{4e00}-\x{9fa5}_]+)$/u", $value);
});
Validator::extend('preference', function($attribute, $value, $parameters, $validator) {
return preg_match("/^(default|slim)$/", $value);
});
Validator::extend('model', function($attribute, $value, $parameters, $validator) {
return preg_match("/^(steve|alex|cape)$/", $value);
});
//
}
/**

View File

@ -2,6 +2,7 @@
namespace App\Providers;
use View;
use Illuminate\Support\ServiceProvider;
use App\Exceptions\PrettyPageException;
@ -14,7 +15,8 @@ class BootServiceProvider extends ServiceProvider
*/
public function boot()
{
\View::addExtension('tpl', 'blade');
View::addExtension('tpl', 'blade');
$this->checkFileExists();
$this->checkDbConfig();
$this->checkInstallation();
@ -47,7 +49,7 @@ class BootServiceProvider extends ServiceProvider
protected function checkInstallation()
{
if (!$this->checkTableExist()) {
\Http::redirect(url('/setup/index.php'));
return redirect('/setup/index.php')->send();
}
if (!is_dir(BASE_DIR.'/storage/textures/')) {
@ -56,7 +58,7 @@ class BootServiceProvider extends ServiceProvider
}
if (config('app.version') != \Option::get('version', '')) {
\Http::redirect(url('/setup/update.php'));
return redirect('/setup/update.php')->send();
}
return true;

View File

@ -0,0 +1,61 @@
<?php
namespace App\Providers;
use Validator;
use Illuminate\Support\ServiceProvider;
class ValidatorExtendServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
/**
* @param $a attribute
* @param $value value
* @param $p parameters
* @param $v validator
*/
Validator::extend('username', function($a, $value, $p, $v) {
return preg_match("/^([A-Za-z0-9\x{4e00}-\x{9fa5}_]+)$/u", $value);
});
Validator::extend('nickname', function($a, $value, $p, $v) {
return $value == addslashes(trim($value));
});
Validator::extend('no_special_chars', function($a, $value, $p, $v) {
return $value == addslashes(trim($value));
});
Validator::extend('playername', function($a, $value, $p, $v) {
return preg_match("/^([A-Za-z0-9_]+)$/", $value);
});
Validator::extend('pname_chinese', function($a, $value, $p, $v) {
return preg_match("/^([A-Za-z0-9\x{4e00}-\x{9fa5}_]+)$/u", $value);
});
Validator::extend('preference', function($a, $value, $p, $v) {
return preg_match("/^(default|slim)$/", $value);
});
Validator::extend('model', function($a, $value, $p, $v) {
return preg_match("/^(steve|alex|cape)$/", $value);
});
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}

View File

@ -164,7 +164,8 @@ return [
App\Providers\AppServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
App\Providers\ResponseMacroServiceProvider::class
App\Providers\ResponseMacroServiceProvider::class,
App\Providers\ValidatorExtendServiceProvider::class,
],