Crud: upload field required

Created on 30 Oct 2017  路  10Comments  路  Source: Laravel-Backpack/CRUD

How do you validate a required upload field. When you set a validation on the field name with an existing value then an the required error shows but there is a value for the field.?

question

Most helpful comment

I found a solution to this. On create use the "required" rule. On update us the "file" rule. This ensures that a file must be attached. I would recommend adding this to the documentation.

All 10 comments

I found a solution to this. On create use the "required" rule. On update us the "file" rule. This ensures that a file must be attached. I would recommend adding this to the documentation.

@kevinab thanks for sharing your solution. Will definitely help others that search ;-) Cheers!

@kevinab how do you set different rules for each request?

@diegoc327 you can have different validation rules in your create and update methods. I don't put my rules in the request class.

@diegoc327 you can create a separate FormRequest file for Store, and another for Update. By default generated CRUDs ship with just one, but you should be able to easily duplicate the one you have, rename it and reuse it in your EntityCrudController. For example, by default you'd have MonsterRequest which is used for both Store and Update:

screen shot 2018-02-21 at 17 59 53

But you can:

  1. Go to your app/Http/Requests;
  2. Rename this to StoreMonsterRequest.php;
  3. Create another file just like it named UpdateMonsterRequest.php;
  4. Change MonsterCrudController appropriately:
-use App\Http\Requests\MonsterRequest as StoreRequest;
+use App\Http\Requests\StoreMonsterRequest as StoreRequest;
-use App\Http\Requests\MonsterRequest as UpdateRequest;
+use App\Http\Requests\UpdateMonsterRequest as UpdateRequest;

Backpack will then pick up different requests for the Store and Update operations.

Hope it helps you or someone else that searches the same thing.
Cheers!

thank you very much @tabacitu very helpful!

I created two new files
CreateProductoRequest.php
UpdateProductoRequest.php

Modify ProductoCrudController to:
//use App\Http\Requests\ProductoRequest as StoreRequest;
//use App\Http\Requests\ProductoRequest as UpdateRequest;
use App\Http\Requests\CreateProductoRequest as StoreRequest;
use App\Http\Requests\UpdateProductoRequest as UpdateRequest;

Also I deleted ProductoRequest.php file

But I get 403 error when run Create or Update products
Seems is forbidden access, perhaps I need declare them in Routes/backpack/custom.php?

@Manolek1975 I don't think the 403 error is related to the FormRequests. I recommend you look at other changes you've made. The error page should give the exact line where the 403 error is triggered.

I really only made those changes, created the two files, and modified the use clauses, in fact I just reverted the changes and with ProductRequest.php it works perfectly, except my problem with unique and Update, I need separate rules for store and update

Page error only show "ERROR 403 Forbidden. This action is unauthorized."

Laravel log, don't show any error =(

Sorry I got error finally, I created files with artisan
php artisan make:request CreateProductoRequest.php

This way function authorized return false, I change function to:
public function authorize()
{
return backpack_auth()->check();
}

and now works, sorry for waste your time @tabacitu

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jorgepires picture jorgepires  路  3Comments

mklahorst picture mklahorst  路  3Comments

AlexanderWM picture AlexanderWM  路  3Comments

alexgmin picture alexgmin  路  3Comments

sonoftheweb picture sonoftheweb  路  3Comments