Hey, i'm fairly new to backpack
i have a field in my MusicCrudController:
$this->crud->addField([ // Music Upload
'name' => 'music',
'label' => 'Music',
'type' => 'upload',
'upload' => true,
'disk' => 'public',
]);
everything is ok when i add a new Music using CrudController, but when i try to edit i get the following message:
Please fix the following errors:
The music field is required.
even though the field show the address of the uploaded music Musics/9c30dc40684eaefb144fda9aca2ae414.mp3
I'm so frustrated. Can anyone help me with this?
What data save to column "music" in your DB?
@divanski it's Musics/7c871fa2d4b5358d756b7109cc47c438.mp3
Try this:
$this->crud->addField(
[
'name' => 'music',
'label' => 'Music',
'type' => 'upload',
'upload' => true,
'default' => 'Music/default-music.mp3',
'disk' => 'public',
], 'both');
and of cource you can add something simple default-music.mp3 in Folder Music
I tried the upload field type with another CrudController ArticleCrudController and again even though the file is uploaded and data saved on related column in db, I get the same error when i try to edit the entry using CrudController.
I 'n'Checked everything and my field is exactly the same as the docs.
I even tried to Log::info($request) in update function of CrudController but it seems the function won't run because of the error.
Also there is a hidden input below the field:
<input id="music_file_input" name="music" value="Musics/7c871fa2d4b5358d756b7109cc47c438.mp3" class="form-control hidden" type="file">
i just don't get it.
Edit
i just saw your reply and i'm trying it.
@divanski Still no luck...
@jhivan Please give all the code from the file MusicCrudController.php
`
MusicCrudController.php
namespace App\Http\Controllers\Admin;
use Backpack\CRUD\app\Http\Controllers\CrudController;
// VALIDATION: change the requests to match your own file names if you need form validation
use App\Http\Requests\MusicRequest as StoreRequest;
use App\Http\Requests\MusicRequest as UpdateRequest;
class MusicCrudController extends CrudController
{
public function __construct()
{
parent::__construct();
/*
|--------------------------------------------------------------------------
| BASIC CRUD INFORMATION
|--------------------------------------------------------------------------
*/
$this->crud->setModel("App\Music");
$this->crud->setRoute("admin/music");
$this->crud->setEntityNameStrings('music', 'musics');
/*
|--------------------------------------------------------------------------
| COLUMNS AND FIELDS
|--------------------------------------------------------------------------
*/
// ------ CRUD COLUMNS
$this->crud->addColumn([
'name' => 'released_date',
'label' => 'Released Date',
'type' => 'date'
]);
$this->crud->addColumn([
'name' => 'index',
'label' => "Index"
]);
$this->crud->addColumn([
'name' => 'title',
'label' => "Title"
]);
$this->crud->addColumn([
'name' => 'music',
'label' => "Music",
]);
$this->crud->addColumn([
// n-n relationship (with pivot table)
'label' => "Albums", // Table column heading
'type' => "select_multiple",
'name' => 'albums', // the method that defines the relationship in your Model
'entity' => 'albums', // the method that defines the relationship in your Model
'attribute' => "name", // foreign key attribute that is shown to user
'model' => "App\Album", // foreign key model
]);
$this->crud->addColumn([
// n-n relationship (with pivot table)
'label' => "Artists", // Table column heading
'type' => "select_multiple",
'name' => 'artists', // the method that defines the relationship in your Model
'entity' => 'artists', // the method that defines the relationship in your Model
'attribute' => "name", // foreign key attribute that is shown to user
'model' => "App\Artist", // foreign key model
]);
$this->crud->addColumn([
// n-n relationship (with pivot table)
'label' => "Genres", // Table column heading
'type' => "select_multiple",
'name' => 'genres', // the method that defines the relationship in your Model
'entity' => 'genres', // the method that defines the relationship in your Model
'attribute' => "name", // foreign key attribute that is shown to user
'model' => "App\Genre", // foreign key model
]);
$this->crud->addColumn([
'name' => 'status',
'label' => "Status"
]);
$this->crud->addColumn([
'name' => 'featured',
'label' => "Featured",
'type' => "model_function",
'function_name' => 'getFeaturedColumn'
]);
// ------ CRUD FIELDS
$this->crud->addField([ // Music Title
'name' => 'index',
'label' => 'Index',
'type' => 'number',
'placeholder' => 'Index of current music in the album'
]);
$this->crud->addField([ // Music Title
'name' => 'title',
'label' => 'Title',
'type' => 'text',
'placeholder' => 'Music title here'
]);
$this->crud->addField([ // Music Slug
'name' => 'slug',
'label' => "Slug (URL)",
'type' => 'text',
'hint' => 'Will be automatically generated from your title, if left empty.'
// 'disabled' => 'disabled'
]);
$this->crud->addField([ // Music Realease Date Create
'name' => 'released_date',
'label' => 'Released Date',
'type' => 'date_picker',
'value' => date('Y-m-d'),
// optional:
'date_picker_options' => [
'todayBtn' => true,
'format' => 'yyyy-mm-dd'
],
], 'create');
$this->crud->addField([ // Music Realease Date Update
'name' => 'released_date',
'label' => 'Released Date',
'type' => 'date_picker',
// optional:
'date_picker_options' => [
'todayBtn' => true,
'format' => 'yyyy-mm-dd'
],
], 'update');
$this->crud->addField([ // Music Art
'name' => "image",
'label' => "Album Art",
'type' => 'image2',
'upload' => true,
'crop' => true, // set to true to allow cropping, false to disable
'aspect_ratio' => 1, // ommit or set to 0 to allow any aspect ratio
'hint' => 'Will use Album\'s Album Art if left empty',
]);
$this->crud->addField([ // Music Upload
'name' => 'music',
'label' => 'Music',
'type' => 'upload',
'upload' => true,
'disk' => 'public'
], 'update/create/both');
$this->crud->addField([ // Select2Multiple = n-n
'label' => 'Artists',
'type' => 'select2_multiple',
'name' => 'artists',
'entity' => 'artists',
'attribute' => 'name',
'model' => "App\Artist",
'pivot' => true,
]);
$this->crud->addField([ // Select2Multiple = n-n
'label' => 'Albums',
'type' => 'select2_multiple',
'name' => 'albums',
'entity' => 'albums',
'attribute' => 'name',
'model' => "App\Album",
'pivot' => true,
]);
$this->crud->addField([ // Select2Multiple = n-n
'label' => 'Genres',
'type' => 'select2_multiple',
'name' => 'genres',
'entity' => 'genres',
'attribute' => 'name',
'model' => "App\Genre",
'pivot' => true,
]);
$this->crud->addField([ // Select2Multiple = n-n
'label' => 'Tags',
'type' => 'select2_multiple',
'name' => 'tags',
'entity' => 'tags',
'attribute' => 'name',
'model' => "App\Tag",
'pivot' => true,
]);
$this->crud->addField([ // ENUM
'name' => 'status',
'label' => "Status",
'type' => 'enum'
]);
$this->crud->addField([ // CHECKBOX
'name' => 'featured',
'label' => "Featured item",
'type' => 'checkbox'
]);
}
public function store(StoreRequest $request)
{
return parent::storeCrud();
}
public function update(UpdateRequest $request)
{
return parent::updateCrud();
}
}`
@jhivan very strange ...
], 'both');
I'm new whit backpack too but in my code this work whit image field
@divanski Yeah, i've been investigating for hours.
I tried 'both' first and it didn't work.
I don't see how default could be related but since i'm so frustrated and open to any suggestions i tried it and still no luck.
image field works just fine.
Irrelevant
I'm making this project as a sample for some guy and the deadline is sooooooo close :(
@MarcosBL Can you confirm if this is a bug or it's just me?
@jhivan sorry, no idea, I use upload field all the time with no problem at all, at least for images... only thing that comes to my mind.. have you set "Music" as a required attr in Request CRUD ?
@MarcosBL If it's required i get the error we're talking about. If i don't set it as required then everything works fine. But the problem is that it needs to be required. OK then. Sorry for bothering you.
No problem at all, sorry for not being helpful
@MarcosBL no worries. it seems to be the kind of problem that takes a whole week and in the end you find a simple one line code fix. will try again in a few days with a fresh os, fresh apache, php, laravel and backback to see if that fixes the issue.
@jhivan - have you got this figured yet? Curious to see what it was. Just gone through the whole thread, I'm afraid there's nothing for me to say - I have zero ideas.
Cheers!
@tabacitu Thanks for being on every single issue in your package, i wish other package owners were like you :+1: .
Well this is fixed now and i don't have a clue why this issue happened in the first place and how it got solved during development. I'm just happy it's gone. Maybe the new Crud update fixed it, Who knows? :)
@jhivan :-)) got it, thanks. Don't you just love it when issues fix themselves? No need to answer. Cheers!
@jhivan hi ,i have the same prob as you described before and still not got solved, I also have a required field and on edit page got the error, can you give a clue to solve it? thank you !
@tabacitu has anyone face the same prob , seems on edit page the value of input has not set? I do need the upload field and feel frustrated...
@victor886 - sounds to me like a finicky validation rule. What's your request file like?
@tabacitu thanks for your reply! My request file is ordinary file like picture in jpg. My problem is also occured on edit page, I finally edit the upload.blade.php, changing the input type from file to text on edit page and change back use js, like below, it works. I dont think it is your original idea , but I not familar the mechanism of it , can you help get the right code ? thanks again
{{-- Show the file picker on CREATE form. --}}
@if(isset($field['value']) && $field['value']!=null)
type="text"
@else
type="file"
@endif
id="{{ $field['name'] }}_file_input"
name="{{ $field['name'] }}"
value="{{ old($field['name']) ? old($field['name']) : (isset($field['value']) ? $field['value'] : (isset($field['default']) ? $field['default'] : '' )) }}"
@include('crud::inc.field_attributes', ['default_class' => isset($field['value']) && $field['value']!=null?'form-control hidden':'form-control'])
>
I know this is a closed issue, but I found this after searching for a solution for a similar issue. I believe I have worked out the cause of the problem. If you have an upload field named file_upload, the javascript component in backpack will submit this as a single file type input field on create. However, when you edit the file, this field is not present unless you try to upload a new file. As a result, the Laravel validation doesn't see a file_upload key in the request input, and throws the validation error.
The problem then comes if someone clicks the remove button when editing, as the javascript of the form element will create an empty file_upload field, which will trigger the removal of the file through the standard crud process, as this is viewed as an effort to delete the uploaded object.
So the question is then, how to have validation that checks that either the input doesn't have a file_upload attribute (in which case we know that the Crud won't delete the current file), or if it does that it is not empty.
Awesomely, it turns out Laravel has you covered. The validation rule just needs to be:
'file_upload' => 'sometimes|required'
@splatEric - sometimes looks awesome, didn't know that existed, thank you!
I don't think it's a proper solution for the problem at hand, though. So what we'd want in this case is for the upload field to be required on Create, but not on Update. Correct? Using sometimes won't make it required for Create, as far as I understand it. So I think a better solution would be to have two separate requests: MusicCreateRequest and MusicUpdateRequest, with the upload field being required in one, but not the other.
But I might be wrong, and sometimes could work too. As I said - I've just learnt about it.
Thanks, cheers!
My understanding of this issue is that the file should always be required on the record but when the user edits another attribute and doesn't change anything about the file, using required triggers a validation error, even though the file association would remain.
sometimes covers this use case:
When no file has been set in the upload field, the frontend will always post the (in my example) file_upload key as part of the request.
sometimes works by only applying the validation rule when the key its defined for is present in the request.
So in create, if no file has been selected, the upload widget will send an empty file_upload value, triggering the sometimes rule, and therefore triggering the required rule.
In update, because there is a file associated with the record, the upload widget will not submit a file_upload key in the request at all. The absence of this key in the request means that the sometimes rule is not triggered, and therefore we don't get a required error. The current file association remains intact, ensuring that the record maintains its created state.
However, if the display of the current file value is removed, the upload widget inserts an empty file_upload field into the form, ensuring that the key is part of the request. The reason it does is this is to force the crud backend to delete the current associated file (which is the presumed requirement of removing the current file from the field). But if we always want a file associated with the record, this is not good enough, the user must provide a replacement. The sometimes rule is triggered by the presence of the file_upload key in the request, in turn triggering the required validation. Thus it ensures we force the user to upload a new file to replace the one being removed.
Hope that makes sense.
@splatEric - thanks a lot for clearing that up for me. sometimes sound PERFECT then :-) I'll use it myself the next time I need this, thanks a lot for teaching me about it.