问题
Laravel comes with this validation message that shows file size in kilobytes:
file' => 'The :attribute may not be greater than :max kilobytes.',
I want to customize it in a way that it shows megabytes instead of kilobytes. So for the user it would look like:
"The document may not be greater than 10 megabytes."
How can I do that?
回答1:
You can extend the validator to add your own rule and use the same logic without the conversion to kb. You can add a call to Validator::extend
to your AppServiceProvider.
<?php namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Validator::extend('max_mb', function($attribute, $value, $parameters, $validator) {
$this->requireParameterCount(1, $parameters, 'max_mb');
if ($value instanceof UploadedFile && ! $value->isValid()) {
return false;
}
// If call getSize()/1024/1024 on $value here it'll be numeric and not
// get divided by 1024 once in the Validator::getSize() method.
$megabytes = $value->getSize() / 1024 / 1024;
return $this->getSize($attribute, $megabytes) <= $parameters[0];
});
}
}
Then to add the error message you can just add it to your validation lang file.
See the section on custom validation rules in the manual
回答2:
We might be on different page, here is what I am trying to say. I hope this helps. Cheers!
public function rules()
{
return [
'file' => 'max:10240',
];
}
public function messages()
{
return [
'file.max' => 'The document may not be greater than 10 megabytes'
];
}
回答3:
This is how I would do validation using Request with custom validation messages in MB instead of KB:
Create a Request file called StoreTrackRequest.php in App\HTTP\Requests folder with the following content
<?php namespace App\Http\Requests; use App\Http\Requests\Request; class StoreTrackRequest extends Request { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return true; } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ "name" => "required", "preview_file" => "required|max:10240", "download_file" => "required|max:10240" ]; } /** * Get the error messages that should be displayed if validation fails. * * @return array */ public function messages() { return [ 'preview_file.max' => 'The preview file may not be greater than 10 megabytes.', 'download_file.max' => 'The download file may not be greater than 10 megabytes.' ]; } }
Inside the controller make sure the validation is performed through StoreTrackRequest request:
public function store($artist_id, StoreTrackRequest $request) { // Your controller code }
回答4:
Change the string in resources\lang\en\validation.php to
'file' => 'The :attribute may not be greater than 10 Megabytes.',
and define the $rule
as
$rules = array(
'file'=>'max:10000',
);
回答5:
File: app/Providers/AppServiceProvider.php
public function boot()
{
Validator::extend('max_mb', function ($attribute, $value, $parameters, $validator) {
if ($value instanceof UploadedFile && ! $value->isValid()) {
return false;
}
// SplFileInfo::getSize returns filesize in bytes
$size = $value->getSize() / 1024 / 1024;
return $size <= $parameters[0];
});
Validator::replacer('max_mb', function ($message, $attribute, $rule, $parameters) {
return str_replace(':' . $rule, $parameters[0], $message);
});
}
Don't forget to import proper class.
File: resources/lang/en/validation.php
'max_mb' => 'The :attribute may not be greater than :max_mb MB.',
'accepted' => 'The :attribute must be accepted.',
'active_url' => 'The :attribute is not a valid URL.',
...
Change config('upload.max_size')
accordingly.
回答6:
This function works
public function getSize()
{
$mb = 1000 * 1024;
if ($this->size > $mb)
return @round($this->size / $mb, 2) . ' MB';
return @round($this->size / 1000, 2) . ' KB';
}
来源:https://stackoverflow.com/questions/34414278/how-to-change-laravel-validation-message-for-max-file-size-in-mb-instead-of-kb