Laravel font extensions validations

2019-08-19 01:56发布

问题:

I am using Form request validation to validating font extension. I'm uploading font types, and my rules are so:

    public function rules()
    {
        if($this->method() == 'POST') {
            return [

                'fonteot' => 'file|mimetypes:eot,application/vnd.ms-fontobject',
                'fontttf' => 'file|mimetypes:ttf,application/x-font-ttf,font/ttf,application/x-font-truetype',
                'fontwoff' => 'file|mimetypes:woff,application/font-woff,application/x-font-woff,font/woff',
                'fontwoff2' => 'file|mimetypes:woff2,font/woff2',
            ];
        }
        return [
            'fonteot' => 'file|mimetypes:eot,application/vnd.ms-fontobject',
            'fontttf' => 'file|mimetypes:ttf,application/x-font-ttf,font/ttf,application/x-font-truetype',
            'fontwoff' => 'file|mimetypes:woff,application/font-woff,application/x-font-woff,font/woff',
            'fontwoff2' => 'file|mimetypes:woff2,font/woff2',
        ];
    }

According to the documentation, I am using mime types for the files extension but my validator can't pass woff & woff2 and redirect to my form and return error messages:

The fontwoff must be a file of type: woff, application/font-woff, application/x-font-woff, font/woff

The fontwoff2 must be a file of type: woff2, font/woff2

What is the cause of this?

回答1:

For mime type validation, you have two ways to do it in Laravel. One is using the mimetypes another is using mimes. I think here you need mimes because this is the validation that check for file extension.

So,

'fontwoff' => 'file|mimes:woff',

Should work for the files with extension of .woff.