我正在尝试上传多个文件,一切正常,但我无法弄清楚如何重命名数组请求的验证属性
现在我收到了(x可以是任意数字,因为用户可以选择任意数量的文件上传)
documents.x必须是以下类型的文件:png,gif,jpeg,txt,pdf,doc。
我想得到(x从0开始,所以它必须是x + 1)
文档x必须是以下类型的文件:png,gif,jpeg,txt,pdf,doc。
我的要求
class CreatePersonRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
$rules = [ 'name' => 'required|unique:people' ];
$docs = $this->file( 'documents' );
if ( !empty( $docs ) )
{
foreach ( $docs as $key => $doc )
{
$rules += ['documents.'.$key => 'mimes:png,gif,jpeg,txt,pdf,doc'] ;
}
}
return $rules;
}
public function attributes(){ //this isn't working
return [
'documents.*' => 'document/s',
];
}
}
我读到我必须在属性中这样做,但我无法弄清楚如何。任何帮助都是适当的
答案 0 :(得分:0)
我已经成功了,虽然它可能不是最好的解决方案,但它为我工作
class CreatePersonRequest extends FormRequest
{
private $docNumber;
private function setNumber($number) {
$this->docNumber = $number;
}
public function authorize()
{
return true;
}
public function rules()
{
$i = 0;
$rules = [ 'name' => 'required|unique:people' ];
$docs = $this->file( 'documents' );
if ( !empty( $docs ) )
{
foreach ( $docs as $key => $doc )
{
$rules += ['documents.'.$key => 'mimes:png,gif,jpeg,txt,pdf,doc'] ;
$i++;
}
}
$this->setNumber($i);
return $rules;
}
public function attributes(){
$i = $this->docNumber;
$attr = [];
for ($y = 0; $y < $i; $y++){
$attr += ['documents.'.$y=> "document " . ($y + 1) ];
}
return $attr;
}
}