Last active
February 6, 2023 04:10
-
-
Save akmandev/b65b6176e9daa746f46980061979bc48 to your computer and use it in GitHub Desktop.
Laravel 5.1 - File Array Validation + Custom Error Messages for Array Data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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