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.
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...
````php
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;
}
````
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.
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
]);
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.
Most helpful comment
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.