Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

addField type browse file restriction #498

Closed
jhivan opened this issue Feb 23, 2017 · 5 comments
Closed

addField type browse file restriction #498

jhivan opened this issue Feb 23, 2017 · 5 comments

Comments

@jhivan
Copy link

jhivan commented Feb 23, 2017

Hey
I'm new to Backpack and I'm having a great time not reinventing the wheel :). I have this code in one of my CrudControllers:

    $this->crud->addField([ 
        'name' => 'file',
        'label' => 'File',
        'type' => 'browse',
        'mimes_allowed' => '/jpeg' //Looking for something like this
    ]);

My question is how can i restrict file types allowed to upload? Lets say i want user to be able to upload only 'jpeg' or 'mp3'. How can i achieve that?
I've looked up 'vendor/backpack/crud/src/resources/views/fields/browse.blade.php' to set the restriction there but the input use is of 'text' type not 'file' type.

Anyways a big thanks to contributers of this awesome package. peace.

@MarcosBL
Copy link
Contributor

You can of course write your own "upload" field type (take a look at custom field types in the doc)

Or you can hook into the store (new) and update functions inside your WhateverCrudController.php to validate the uploaded files and deny them if they don't comply with your requirements, something along the lines...

public function store(StoreRequest $request)
{
	if (!$this->validate_upload()) { /* Custom function that validates uploaded files mimes */
		return \Redirect::back()->withErrors(\Validator::make(\Input::all(), [])->getMessageBag()->add('tutorias', 'Incorrect file type on create!');)->withInput();
	}
	$redirect_location = parent::storeCrud();
	return $redirect_location;
}

public function update(UpdateRequest $request)
{
	if (!$this->validate_upload()) { /* Custom function that validates uploaded files mimes */
		return \Redirect::back()->withErrors(\Validator::make(\Input::all(), [])->getMessageBag()->add('tutorias', 'Incorrect file type on update!');)->withInput(); }
	}
	$redirect_location = parent::updateCrud();
	return $redirect_location;
}

@jhivan
Copy link
Author

jhivan commented Feb 24, 2017

I knew my question was rather noobish but i was frustrated last night about this. Thank you for your response. I wish you a very good day.

@jhivan
Copy link
Author

jhivan commented Feb 24, 2017

Although i still think mime filter should be added as an extra option. like:

$this->crud->addField([ 
    'name' => 'file',
    'label' => 'File',
    'type' => 'browse',
    'mimes_allowed' => '/jpeg' //Looking for something like this
]); 

@MarcosBL
Copy link
Contributor

No noobish questions, just a desire to learn, that's great.

If you want that, you need to create a custom field type; and even doing so, you'll still have to create a validation, as you can't trust every browser out there to follow your specifications.

@tabacitu tabacitu closed this as completed Mar 9, 2017
@maya-hamdan
Copy link

Its not a complete solution but in the request validation u could do something like this in the rules function:
public function rules() { return [ 'file' => 'ends_with:.jpeg,' ]; }
and in the messages function u could do:
public function messages() { return [ 'file.ends_with' => 'Icon type must be jpeg' ]; }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants