admin icon indicating copy to clipboard operation
admin copied to clipboard

Upload validation issue in FormItem::file

Open livan2r opened this issue 10 years ago • 7 comments

I found an issue in FormItem::file upload validation (V3 version), i only can upload image files, for example if i try to upload a zip file i get a validation error with the message 'The file must be an image.' See the below image.

upload_file_validation_error

livan2r avatar Aug 08 '15 16:08 livan2r

For me, it looks like, that class File extends Image is the problem.

In the "File" Class, we have the function uploadValidationRules which should overwrite the validation from the Image Class.

What you can try is to change the File Class Definition from:

class File extends Image

to:

class File extends NamedFormItem implements WithRoutesInterface

Maybe this helps - If this fixes the bug - You can create a PR :)

Hope this helps.

noxify avatar Aug 10 '15 09:08 noxify

Noxify, thanks for the clue. I built this solution with your recommendation, I think there's some duplicate code but while a more optimal solution appears, this is doing the job.

I replaced the code of ..../vendor/sleeping-owl/admin/src/SleepingOwl/Admin/FormItems/File.php for this:

<?php namespace SleepingOwl\Admin\FormItems;

use Input;
use Response;
use Route;
use SleepingOwl\Admin\AssetManager\AssetManager;
use SleepingOwl\Admin\Interfaces\WithRoutesInterface;
use Validator;

class File extends NamedFormItem implements WithRoutesInterface
{

    protected $view = 'file';
    protected static $route = 'uploadFile';

    public function initialize()
    {
        parent::initialize();

        AssetManager::addScript('admin::default/js/formitems/image/init.js');
        AssetManager::addScript('admin::default/js/formitems/image/flow.min.js');
    }

    public static function registerRoutes()
    {
        Route::post('formitems/image/' . static::$route, [
            'as' => 'admin.formitems.image.' . static::$route,
            function ()
            {
                $validator = Validator::make(Input::all(), static::uploadValidationRules());
                if ($validator->fails())
                {
                    return Response::make($validator->errors()->get('file'), 400);
                }
                $file = Input::file('file');
                $filename = md5(time() . $file->getClientOriginalName()) . '.' . $file->getClientOriginalExtension();
                $path = config('admin.imagesUploadDirectory');
                $fullpath = public_path($path);
                $file->move($fullpath, $filename);
                $value = $path . '/' . $filename;
                return [
                    'url'   => asset($value),
                    'value' => $value,
                ];
            }
        ]);
    }

    protected static function uploadValidationRules()
    {
        return [
            'file' => 'required',
        ];
    }

}

livan2r avatar Aug 10 '15 17:08 livan2r

The issue here is how static late binding works with different PHP versions, in closure functions. On PHP 5.5.9, static:: is always the Image class in the route closure, when the closure is invoked.

Below is a small script to test your version. It should output:

Base
Direct: extra
Callback: extra

Extra
Direct: extra
Callback: extra

However, on PHP 5.5.9 (latest on Ubuntu 14.04 LTS), it outputs:

Base
Direct: extra
Callback: base

Extra
Direct: extra
Callback: base

<?php

class Base
{
    protected static $prop = 'base';

    public static function run()
    {
        echo 'Direct: ' . static::$prop . PHP_EOL;
        $array = ['one'];
        array_walk($array, function () {
            echo 'Callback: ' . static::$prop . PHP_EOL;
        });
    }
}

class Extra extends Base
{
    protected static $prop = 'extra';
}

echo 'Base' . PHP_EOL;
call_user_func(['Extra', 'run']);
echo PHP_EOL;
echo 'Extra' . PHP_EOL;
call_user_func(['Extra', 'run']);

dandarie avatar Feb 16 '16 20:02 dandarie

а как сделать чтобы FormItem::image('preview', 'Превью')->required(), могла брать картинки из уже загруженных?

alexspi avatar Feb 19 '16 15:02 alexspi

Sorry, I'm not sure what you're asking.

Please reply in English.

dandarie avatar Feb 19 '16 15:02 dandarie

how to make that Form Item :: image ( 'preview', 'Preview') -> required (), I could take pictures of the already loaded?

alexspi avatar Feb 20 '16 07:02 alexspi

В модели нужно создать функцию: public function getPreviewAttribute(){ return .....; }

panchenko91 avatar Jun 02 '17 08:06 panchenko91