Crud: addField type browse file restriction

Created on 23 Feb 2017  路  4Comments  路  Source: Laravel-Backpack/CRUD

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.

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.

All 4 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mklahorst picture mklahorst  路  3Comments

M0H3N picture M0H3N  路  3Comments

gotrecillo picture gotrecillo  路  3Comments

sokvebolkol picture sokvebolkol  路  3Comments

abewartech picture abewartech  路  3Comments