在我所使用的服务器上没有启用FILE_INFO之后,我需要一种快速验证word文档的方法。
Validator::register( 'word', function( $attribute, $value, $parameters )
{
$valid_type = array(
'application/msword',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
);
$valid_extentions = array(
'doc',
'docx'
);
if( ! is_array( $value ) )
{
return false;
}
if( ! isset( $value['type'] ) )
{
return false;
}
if( ! in_array( strtolower( $value['type'] ), $valid_type ) )
{
return false;
}
if( ! in_array( strtolower( substr( strrchr( $value['name'], '.' ) , 1 ) ), $valid_extentions ) )
{
return false;
}
return true;
});
我知道这不是防弹,但现在会做(如果你有的话可以添加建议)但是如何为此添加消息,因为它现在返回
validation.word
任何想法?
答案 0 :(得分:3)
要将消息全局添加到“url”之后的主数组中的 /app/lang/en/validation.php ,
<?php
return array(
//...
"url" => "The :attribute format is invalid.",
"word" => "The document must be a Microsoft Word-file.",
//..
要使自定义验证规则全局使用 /app/validators.php 并添加如下内容:
<?php
class CustomValidator extends Illuminate\Validation\Validator
{
//validate foo_bar
public function validateFooBar($attribute, $value, $parameters)
{
return ($value == 'foobar');
}
}
Validator::resolver(function($translator, $data, $rules, $messages)
{
return new CustomValidator($translator, $data, $rules, $messages);
});
答案 1 :(得分:1)
您必须定义新的验证规则和消息。
自定义规则如下所示:
$rules = array(
'input_file' => 'required|word',
);
消息如下所示:
$messages = array(
'word' => 'The document must be .doc!',
);
最后,您必须使用规则和消息调用验证器:
$validator = Validator::make(Input::get(), $rules, $messages);
查看官方文档Custom Validation