Support the ongoing development of Laravel.io →
posted 9 years ago
Validation
Last updated 2 years ago.
0

For more info, here's my action:

    public function store(VideoRequest $request)
    {
        $video = new Video($request->all());
        Auth::user()->videos()->save($video);
        $this->uploadVideo($request);
        flash()->success('Vidéo ajoutée avec succès.');
        return redirect('videos');
    }

And here's my FormRequest:

class VideoRequest extends Request {
	public function authorize()
	{
		// if logged in
		return \Auth::check();
	}

	public function rules()
	{
		$rules = [
			'title' => 'required:min:8',
		];

		// on create
		if ($this->isMethod('POST')) {
			$rules['video'] = 'mimes:m4v,avi,flv,mp4,mov';
		}

		// on update
		if ($this->isMethod('PATCH')) {
			// todo: or when uploading a new one (update)?
		}

		return $rules;
	}
}

FORM open tag: {!! Form::open(['route' => 'videos.store', 'method' => 'POST', 'files' => true]) !!}

and video file tag: {!! Form::file('video', null, ['class' => 'form-control']) !!}

Hope this helps, thanks!

0

Anybody?

0

I'm having a problem getting the mimes validation working too...

I think I'm going to make a custom validation rule and just check file extensions...

0

I will be talking about request validation: Use i.e. "jpeg" instead of "image/jpeg" in rules() function (it can validate "mimes" of file just like other field). "image/jpeg" won't work. Use only sub-type of MIME and it will work. I just tried this (I had the same problem).

public function rules()
	{
        return [
            'file' => 'required|mimes:jpeg',
        ];
	}
Last updated 9 years ago.
0

Hi,

Are you using the PHP built in server? The built in server supports a limited number of mime types, depending on which PHP you are running.

http://php.net/manual/en/features.commandline.webserver.php

0

Sorry if I am necroposting but I just ran into this issue a couple of days ago and it has been driving me nuts. The validator keeps returning errors for a CSV file that I am trying to upload, for some reason it keeps saying my CSV file is text/plain. By stepping through the code within a debugger I find that internally the mimes validation rule uses Symfony\Component\HttpFoundation\File::getMimeType() which is written as follows:

    public function getMimeType()
    {
        $guesser = MimeTypeGuesser::getInstance();

        return $guesser->guess($this->getPathname());
    }

The problem here is that the $guesser->guess() call uses Symfony\Component\HttpFoundation\File\MimeType\FileinfoMimeTypeGuesser which uses PHP's fileinfo extension and falls back to Symfony\Component\HttpFoundation\File\MimeType\FileBinaryMimeTypeGuesser which just uses "file -b --mime %s" to try to guess the file that is in $this->getPathname(). So by default the validator is dependent whatever file magic data your system has in its /usr/share/magic/ directory. I ended up writing my own custom validator to check the CSV file as I needed to check the CSV file structure for validity anyway.

I hope this helps anyone who happens to come across this issue.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

tbergeron tbergeron Joined 6 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.