我得到了一个有效的解决方案。 我的想法如下:如果有张贴的图像,然后使用$ validator-> fails()方法,我从服务器上删除图像。
下面是完整的代码,如果有人需要它:
// Validating the request
public function validate(Request $request){
$rules = []; //define your rules
$messages = []; //define your rules
// We make new validator with request data, rules and messages
$validator = Validator::make($request->all(), $rules, $messages);
// Deleting images if the validator fails
if($validator->fails()){
if(isset($request->images)){
foreach($request->images as $image){
// unlink your image here
}
}
return Redirect::back()->withInput()->withErrors($validator);
}
}
则在该方法存储/编辑(或自定义方法)只要致电:
public function store(Request $request){
// If it fails, return the redirect we defined.
if($this->validate($request)){
return $this->validate($request);
}
}
您也可以定义验证直接在商店方法中,但由于我在我的控制器中使用它几次,我为它做了一个单独的方法。如果我们想重定向错误,我们必须返回它的值。
干杯。
验证是为图像?或表单中的其他字段绑定? – 2Fwebd
其他领域。我使用AJAX验证图像输入,我呼吁上传/删除图像。当我在dropzone中上传图片并成功上传时,会生成一个输入标签,名称=“images []”,然后我处理控制器中的图像并将它们存储在数据库中。 (从dropzone中删除图像时删除输入标签) –