我想添加多个自定义验证,每个都有自己的文件。
到目前为止,我已经修改了我的
到目前为止,我已经修改了我的
app/start/global.php
文件。
global.php
ClassLoader::addDirectories(array(
app_path().'/commands',
app_path().'/controllers',
app_path().'/models',
app_path().'/database/seeds',
app_path().'/validators' // <--- Added this folder
));
// Only the first resolver works.
// I cannot seem to have multiple of these files
Validator::resolver(function($translator, $data, $rules, $messages)
{
return new ScheduleValidator($translator, $data, $rules, $messages);
});
Validator::resolver(function($translator, $data, $rules, $messages)
{
return new UserValidator($translator, $data, $rules, $messages);
});
我的每个验证文件都会在/validators
中。
ScheduleValidator.php
class ScheduleValidator extends Illuminate\Validation\Validator
{
protected $implicitRules = array('Required', 'RequiredWith', 'RequiredWithout', 'RequiredIf', 'Accepted', 'RequiredWithoutField');
public function __construct(\Symfony\Component\Translation\TranslatorInterface $translator, $data, $rules, $messages = array())
{
parent::__construct($translator, $data, $rules, $messages);
$this->isImplicit('fail');
}
/**
* Validates type to be of the type 'common', 'template', or 'revision'
*/
public function validateTypeSchedule($attribute, $value, $parameters = null)
{
$valid_types = ['template', 'common', 'revision'];
return in_array($value, $valid_types);
}
// and the other validators ...
}
那么我该如何添加多个验证器呢?