Open rahman-nero opened 1 week ago
Hello there! Thanks for opening your first issue on this repo!
Just a heads-up: Here at Backpack we use GitHub Issues only for tracking bugs. Talk about new features is also acceptable. This helps a lot in keeping our focus on improving Backpack. If you issue is not a bug/feature, please help us out by closing the issue yourself and posting in the appropriate medium (see below). If you're not sure where it fits, it's ok, a community member will probably reply to help you with that.
Backpack communication channels:
backpack-for-laravel
tag;Please keep in mind Backpack offers no official / paid support. Whatever help you receive here, on Gitter, Slack or Stackoverflow is thanks to our awesome awesome community members, who give up some of their time to help their peers. If you want to join our community, just start pitching in. We take pride in being a welcoming bunch.
Thank you!
-- Justin Case The Backpack Robot
I checked the theory of select2_multiple
being the problem but I was wrong.
I redefined gkCitiesFlex
without specifying the type:
[
'name' => 'prices',
'type' => 'relationship',
'label' => '...',
'tab' => '...',
'subfields' => [
[
'name' => 'gkCitiesFlex',
'label' => '...',
'wrapper' => [
'class' => 'form-group col-sm-12',
],
...
],
It doesn't work as well. Got the same error
I think I found what might cause this problem.
Before sending form, I deleted some existing items (prices)
If I remove prices - I get the error. However, if I just add new prices, I don't get the error. Like deleting existing ones cause the problem. I am sure 100% of it
I recorded video: https://drive.google.com/file/d/1ZXB8KPlQCZyxskH46YV76euQaNCCQt7S/view?usp=sharing
Hello @rahman-nero thanks for the report and all the details.
Where do you get the gkCitiesFlex
options for the select ? What kind of relationship is prices
? What's your validation rule for that field ?
Cheers
@pxpm
Main model Order
where I defined prices
:
/**
* Prices
* @return HasMany
*/
public function prices(): HasMany
{
return $this->hasMany(WorkTypePrice::class);
}
WorkTypePrice
model:
class WorkTypePrice extends Model
{
use CrudTrait;
protected $fillable = [
'order_id',
'contr_type_id',
'business_type_id',
'connection_type_id',
'price',
'date_start',
'date_end',
];
/**
* Relationship to Cities through pivot-table.
* @return BelongsToMany
*/
public function gkCitiesFlex(): BelongsToMany
{
$model = new WorkTypePriceGkCity();
$table = "{$model->getConnection()->getDatabaseName()}.{$model->getTable()}";
return $this->belongsToMany(GkCitiesFlex::class, $table);
}
}
WorkTypePriceGkCity (just pivot-table) between cities and prices. One price can have multiple cities:
class WorkTypePriceGkCity extends Model
{
protected $fillable = [
'work_type_price_id',
'gk_cities_flex_id',
];
/**
* @return BelongsTo
*/
public function gkCitiesFlex(): BelongsTo
{
$model = new GkCitiesFlex();
$table = "{$model->getConnection()->getDatabaseName()}.{$model->getTable()}";
return $this->belongsTo($table);
}
}
I think the validation doesn't matter but here is:
'prices' => [
'nullable',
'array',
$this->checkIfPricesValidate(...),
],
'prices.*.gkCitiesFlex' => [
'required',
'array',
],
'prices.*.contr_type_id' => [
'nullable',
'in:' . $this->createInRule(ToServiceTypeField::CONTR_TYPE),
],
'prices.*.business_type_id' => [
'nullable',
'in:' . $this->createInRule(ToServiceTypeField::BUSINESS_TYPE),
],
'prices.*.connection_type_id' => [
'nullable',
'in:' . $this->createInRule(ToServiceTypeField::CONNECTION_TYPE),
],
'prices.*.price' => [
'required',
'numeric',
'min:0',
'regex:/^\d+(\.\d{1,2})?$/'
],
'prices.*.date_start' => [
'required',
'date_format:Y-m-d',
],
'prices.*.date_end' => [
'required',
'date_format:Y-m-d',
'after_or_equal:prices.*.date_start',
]
I also do custom checking for prices
, but it doesn't matter, even if I remove it, the validation will fail due to not being an array.
Hello! @pxpm
I would like to know if there is any updates on the issue?
Bug report
What I did
There is a particular bug I hate the most in Backpack.
So I have 'relationship'-type field with subfields. I wrote some validation for them as well, like checking whether it is unique or not. So when I open edit-page, edit those fields and send form, I expect from
select2_multiple
(gkCitiesFlex) field to be an array, but Backpack sends me the field as string although it's clearly an array. My validation fails because of that.In my controller. I have defined these fields on
setupUpdateOperation
:Here is what Backpack sends my after I submit form:
As you can see, in the first element of prices, it contains
gkCitiesFlex
as string, it's clearly wrong, It must be an array (because I selected like 25 values for the field in the form). I don't know why it does it.I saw this bug even in backpack/crud 5 version, I thought it would be fixed in 6 version
I am attaching a screenshot where I show how the form was filled and sent
What I expected to happen
I expect
gkCitiesFlex
to be an array cuz I defined it asselect2_multiple
.What happened
Backpack sends an array as string, taking last element from selected values and gives it as value (string).
Backpack, Laravel, PHP, DB version
When I run
php artisan backpack:version
the output is: