Mae*_*aeh 9 php validation laravel laravel-5
我正在将我的Laravel应用程序从4升级到5.但是,我有一个我无法工作的自定义验证器.
在L4中,我制作了一个validators.php文件并将其包含在global.php中require app_path().'/validators.php';.
我尝试在L5中做同样的事情.我在app/Validators/Validators.php中删除了一个验证器,并更新了我的composer.json.
"files": [
"app/Validators/Validators.php"
]
Run Code Online (Sandbox Code Playgroud)
但是,现在在任何页面上都没有渲染.我做错了什么?
man*_*nix 33
请尝试以下方法:
Validator类的每个规则.ServiceProvider.config/app.php文件中添加自定义验证程序提供程序.您可以在Services文件夹中创建绑定,如下所示:
namespace MyApp\Services;
class Validator extends \Illuminate\Validation\Validator{
public function validateFoo($attribute, $value, $parameters){
return $value == "foo"
}
}
Run Code Online (Sandbox Code Playgroud)
然后,使用服务提供商扩展核心:
namespace MyApp\Providers;
use MyApp\Services\Validator;
use Illuminate\Support\ServiceProvider;
class ValidatorServiceProvider extends ServiceProvider{
public function boot()
{
\Validator::resolver(function($translator, $data, $rules, $messages)
{
return new Validator($translator, $data, $rules, $messages);
});
}
public function register()
{
}
}
Run Code Online (Sandbox Code Playgroud)
最后,config/app.php像这样导入您的服务提供商:
'providers' => [
...
...
'MyApp\Providers\ValidatorServiceProvider';
]
Run Code Online (Sandbox Code Playgroud)
所以这就是我在添加自定义验证时所做的.这是laravel 5.1
PHP Artisan make:request MyFormValidationRequest文件是在.下创建的app\Requests\MyFormValidationRequest.php这是初始代码:
<?php
namespace App\Http\Requests;
use App\Http\Requests\Request;
class MyFormValidationRequest extends Request
{
/**
* 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 [
//
];
}
}
Run Code Online (Sandbox Code Playgroud)
重要信息:authorize()如果您没有进行任何身份验证,请将方法的返回值更改为true.它的初始值是假的.否则你会得到一个带有"Forbidden"错误消息的白页.
我在函数下添加了一条规则rules(),这就是它的样子
public function rules() {
return [
'activeuntil' => 'today_onwards'
];
}
Run Code Online (Sandbox Code Playgroud)today_onwards 是我的新验证.
我在App文件夹下创建了一个名为"Services"的文件夹
我在App\Services文件夹下创建了一个名为'ValidatorExtended.php'的文件,下面的代码如下:
<?php
namespace App\Services;
use Illuminate\Validation\Validator;
use Carbon\Carbon;
class ValidatorExtended extends Validator {
private $_custom_messages = array(
"today_onwards" => "The :attribute must be today onwards",
);
public function __construct( $translator, $data, $rules, $messages = array(), $customAttributes = array() ) {
parent::__construct( $translator, $data, $rules, $messages, $customAttributes );
$this->_set_custom_stuff();
}
protected function _set_custom_stuff() {
//setup our custom error messages
$this->setCustomMessages( $this->_custom_messages );
}
protected function validateTodayOnwards( $attribute, $value ) {
$now = strtotime('-1 day');
$valueDateFormat = strtotime($value);
if($valueDateFormat > $now){
return true;
}
else {
return false;
}
}
}
Run Code Online (Sandbox Code Playgroud)注意: validateTodayOnwards方法是您放置逻辑的地方.该方法的名称应始终以"validate"开头,然后是新的验证密钥的名称,该名称应为标题大小写,
另请注意,您的验证密钥应以下划线和所有小写字母分隔,在本例中为"today_onwards".下划线应放在方法名称中的所有首字母大写字母之前.我希望我解释得很好.
TodayOnwards方法相当于"today_onwards"的验证名称,
另一个例子,如果我创建了validateOldPassword,那么你的验证密钥应该是"old_password".
我在app\Providers\AppServiceProvider.phpinside boot()方法中添加了以下代码.
Validator::resolver(function($translator, $data, $rules, $messages = array(), $customAttributes = array())
{
return new ValidatorExtended($translator, $data, $rules, $messages, $customAttributes);
});
Run Code Online (Sandbox Code Playgroud)不要忘记添加下面的库,一个是Validator类,另一个是你自己的类,是" ValidatorExtended".
use App\Services\ValidatorExtended;
use Illuminate\Support\Facades\Validator;
Run Code Online (Sandbox Code Playgroud)这是整个文件的样子,[ app\Providers\AppServiceProvider.php]
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Services\ValidatorExtended;
use Illuminate\Support\Facades\Validator;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
Validator::resolver(function($translator, $data, $rules, $messages = array(), $customAttributes = array())
{
return new ValidatorExtended($translator, $data, $rules, $messages, $customAttributes);
});
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}
Run Code Online (Sandbox Code Playgroud)而已.完成.您创建了自己的自定义验证.
此外,如果您想在控制器中使用它,下面是代码:
class testController extends Controller
{
public function updatePass(MiscValidation $request){
//code here
}
}
Run Code Online (Sandbox Code Playgroud)您可以使用自己的类,而不是使用Request Class,它是Request类的扩展.
| 归档时间: |
|
| 查看次数: |
12440 次 |
| 最近记录: |