2 php validation laravel laravel-4
要向Laravel添加新验证,我已经这样做了:
做了一个所谓的新文件customValidation.php
中app/start/
然后把它包括进来app/start/global.php
并用echo()
它测试它并且它起作用了.所以它现在被加载到应用程序中.
然后我编写了以下代码来验证Laravel中的复选框:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
class customValidate extends Illuminate\Validation\Validator
{
public function validateCheckbox($attribute, $value, $parameters)
{
//if(isset($value)) { return true; } else { return true; }
echo "this is the: " . $value;
}
}
/**
* Resolvers for Custom Validations
*/
Validator::resolver(function($translator, $data, $rules, $message){
return new customValidate($translator, $data, $rules, $message);
});
Run Code Online (Sandbox Code Playgroud)
但是,在我的验证规则中,我定义:
`array('sex'=>'checkbox')`
Run Code Online (Sandbox Code Playgroud)
但它不起作用.什么都没发生.不会抛出任何错误.该应用程序的行为就像它根本没有执行该功能一样.此外,当我从函数内部回显一些东西时,没有任何东西得到回应,这是函数根本没有被调用的另一个证据.
我会为此创建自定义app/validators
文件夹.
1,创建app/validators/CustomValidate.php
<?php
class CustomValidate extends Illuminate\Validation\Validator
{
public function validateCheckbox($attribute, $value, $parameters)
{
echo "this is the: " . $value;
}
}
Run Code Online (Sandbox Code Playgroud)
2,跑php artisan optimize
或composer dumpautoload
3, Somewhere注册您的自定义验证器.也许添加app/validators.php
到start/global.php
Validator::resolver(function($translator, $data, $rules, $message){
return new CustomValidate($translator, $data, $rules, $message);
});
Run Code Online (Sandbox Code Playgroud)
4,验证
$rules = ['agreed' => 'checkbox'];
$data = ['agreed' => 0];
$v = Validator::make($data, $rules);
dd($v->passes());
Run Code Online (Sandbox Code Playgroud)