Laravel-图像阵列验证给可为空的验证提供了一个错误 [英] Laravel - Image Array Validation gives an Error to nullable validate
问题描述
我需要将图像阵列验证为仅图像和特定图像文件扩展名.但是我对图像的请求验证允许我使用插入的可为空的值
I need to validate my image array as an image and specific image file extensions only. but my request validation to image WONT ALLOW me to use inser nullable values
例如,我将添加内容,但不想添加图像.然后图像应包含null,这就是为什么我需要将请求验证为可为null的原因.但是根据我的经验,不允许使用null值,这会导致我出错为什么?请帮助我
For example I will add a content and dont want to add images. then the image should contain null that is why i need to have request validation as nullable. But in my experience null value is not allowed and it gives me error why? help me please
这是错误.
促销图片必须是图片
The Promotion Image must be an Image
这是我的 CONTROLLER
here is my CONTROLLER
public function store(Request $request)
{
$this->validate($request, [
'promotion_image' => 'image|nullable|max:1999'
]);
$promotion = [];
if ($request->has('promotion_image'))
{
//Handle File Upload
foreach ($request->file('promotion_image') as $key => $file)
{
// Get FileName
$filenameWithExt = $file->getClientOriginalName();
//Get just filename
$filename = pathinfo( $filenameWithExt, PATHINFO_FILENAME);
//Get just extension
$extension = $file->getClientOriginalExtension();
//Filename to Store
$fileNameToStore = $filename.'_'.time().'.'.$extension;
//Upload Image
$path = $file->storeAs('public/promotion_images',$fileNameToStore);
array_push($promotion, $fileNameToStore);
}
$fileNameToStore = serialize($promotion);
}
else
{
$fileNameToStore='noimage.jpg';
}
if (count($promotion)) {
$implodedPromotion = implode(' , ', $promotion);
$promotionImage = new Promotion;
$promotionImage->promotion_image = $implodedPromotion;
$promotionImage->save();
return redirect('/admin/airlineplus/promotions')->with('success', 'Image Inserted');
}
return redirect('/admin/airlineplus/promotions')->with('error', 'Something went wrong.');
}
这是我的 VIEW
here is my VIEW
{!! Form::open(['action'=>'Admin\PromotionsController@store', 'method' => 'POST','enctype'=>'multipart/form-data', 'name' => 'add_name', 'id' => 'add_name']) !!}
<div class="form-group">
<div class="table-responsive">
<table class="table table-bordered" id="dynamic_field">
<tr>
<td> {{ Form::file('promotion_image[]')}}</td>
<td>{{ Form::button('', ['class' => 'btn btn-success fa fa-plus-circle', 'id'=>'add','name'=>'add', 'style'=>'font-size:15px;']) }}</td>
</tr>
</table>
{{Form::submit('submit', ['class'=>'btn btn-primary', 'name'=>'submit'])}}
</div>
</div>
{!! Form::close() !!}
推荐答案
无需在验证中添加可为空的属性.只需像这样更改您的验证码
No need to add nullable attribute in the validation. just change your validation code like this
$this->validate($request, [
'promotion_image.*' => 'image|max:1999'
]);
如果需要用户必须添加图像输入,则可以使用required
验证规则,否则就不需要这种东西.
If you need user must add image input then you can use required
validation rule other wise you don't need such thing.
以上代码强制用户添加图像类型的文件或完全不添加任何文件.
Above code forces user to add file of type image or nothing at all.
希望您能理解,如果需要任何解释,请随时提问.
I hope you understand and if any explanation needed, feel free to ask.
这篇关于Laravel-图像阵列验证给可为空的验证提供了一个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!