Skip to content

Instantly share code, notes, and snippets.

@akmandev
Last active February 6, 2023 04:10
Show Gist options
  • Save akmandev/b65b6176e9daa746f46980061979bc48 to your computer and use it in GitHub Desktop.
Save akmandev/b65b6176e9daa746f46980061979bc48 to your computer and use it in GitHub Desktop.
Laravel 5.1 - File Array Validation + Custom Error Messages for Array Data
<?php
$rules = [
'name' => 'required',
'email' => 'required|email',
//etc..
];
foreach($request->file('files') as $index => $file) {
$rules['files.' . $index] = 'mimes:jpeg,gif,png,pdf';
$messages['files.' . $index . '.mimes'] = 'All files must be a file of type: <strong>jpg, jpeg, gif, png</strong> or <strong>pdf</strong>';
}
$validator = Validator::make($post, $rules, $messages);
if($validator->fails()){
return redirect()
->back()
->withErrors($validator)
->withInput();
}else{
// do smt
}
/* For view file you must do something like this => array_unique($error->all()) otherwise messages will repeat theirself */
@foreach (array_unique($errors->all()) as $error)
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment