Upload validation issue in FormItem::file
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.

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, 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',
];
}
}
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']);
а как сделать чтобы FormItem::image('preview', 'Превью')->required(), могла брать картинки из уже загруженных?
Sorry, I'm not sure what you're asking.
Please reply in English.
how to make that Form Item :: image ( 'preview', 'Preview') -> required (), I could take pictures of the already loaded?
В модели нужно создать функцию: public function getPreviewAttribute(){ return .....; }