使用laravel对上传的文件进行验证



如何在文件中添加验证角色,在这个例子中:

public function store( Product $product, Request $request)
{
$this->validate($request, [
'info' => ['required'],
'min' =>['required'],
'max' => ['required'],
'file' => ['nullable', 'mimes:jpg,jpeg,png,PNG,doc,docx,pdf,xsl,xlsx'],
]);
//store
if($request->hasFile('file')) {
$dest_path = 'public/orders/files';
$order_file = $request->file('file');
$file_name = time().'.'.$order_file->getClientOriginalName();
$order_file->storeAs($dest_path, $file_name);
try {
Order::create([
'info' => $request->info,
'min' => $request->min,
'max' => $request->max,
'file' => $file_name,
'product_id' => $product->id,
'user_id' => auth()->id(),
'owner_id' => $product->user->id
])->save();

在上面的方法中它不起作用…

,这是。blade文件:

<form action="{{route('order.store', [$product->id])}}" method="post"
enctype="multipart/form-data" class="">
@csrf
<div class="px-4 py-5 sm:p-6 items-center justify-center mb-5">
<label for="info" class="block font-medium text-sm text-gray-700">Details</label>
<textarea rows="5" name="info" id="info"
class="rounded-md shadow-sm mt-1 block w-full p-5"
placeholder="write any information about your order"></textarea>
@error('info')
<p class="text-sm text-red-600">{{ $message }}</p>
@enderror
</div>

<div class="flex flex-wrap -mx-3 mb-6">
<div class="w-full md:w-1/2 px-3 mb-6 md:mb-0">
<label class="block tracking-wide text-gray-700 text-xs font-bold mb-2" for="grid-name">
Minimum Period (in Days)
</label>
<input name="min" value="{{old('min')}}" class="@error('min') border-red-500 @enderror appearance-none block w-full bg-white text-gray-700 border border-gray-200 rounded py-3 px-4 mb-3 leading-tight focus:outline-none focus:bg-white" id="grid-first-name" type="number" placeholder="30">
@error('min')
<p class="text-red-500 text-xs italic">{{$message}}</p>
@enderror
</div>
<div class="w-full md:w-1/2 px-3">
<label class="block  tracking-wide text-gray-700 text-xs font-bold mb-2" for="grid-email">
Maximum Period (in Days)
</label>
<input name="max" value="{{old('max')}}" class="@error('max') border-red-500 @enderror appearance-none block w-full bg-white text-gray-700 border border-gray-200 rounded py-3 px-4 leading-tight focus:outline-none focus:bg-white focus:border-gray-500" id="grid-email" type="number" placeholder="40">
@error('max')
<p class="text-red-500 text-xs italic">{{$message}}</p>
@enderror
</div>
</div>

<div class="bg-gray-100 text-center justify-center text-black
font-bold border border-red-600 p-5 mb-10">
send a word, pdf, text or image file contains all information about your order, like information
about yourself, if your order is CV, or information, digrams and images ..etc.. about the
application if your order is application.
</div>
<div class="flex w-full h-20 items-center justify-center bg-grey-lighter">
<label
class="w-64 flex flex-col items-center px-4 py-6 bg-white text-blue rounded-lg shadow-lg tracking-wide uppercase border border-blue cursor-pointer hover:bg-blue hover:text-white">
<svg class="w-8 h-8" fill="currentColor" xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20">
<path
d="M16.88 9.1A4 4 0 0 1 16 17H5a5 5 0 0 1-1-9.9V7a3 3 0 0 1 4.52-2.59A4.98 4.98 0 0 1 17 8c0 .38-.04.74-.12 1.1zM11 11h3l-4-4-4 4h3v3h2v-3z"/>
</svg>
<span class="mt-2 text-base leading-normal">Select a file</span>
<input type='file' name="file" class="hidden"/>
</label>
</div>

感谢

您必须检查两件事:

  1. 您的表单有enctype="multipart/form-data"吗?
  2. 检查文件字段名。是file吗?

相关内容

  • 没有找到相关文章

最新更新