open-admin icon indicating copy to clipboard operation
open-admin copied to clipboard

Nested form image type not get stored value in edit action

Open kanishka55 opened this issue 1 year ago • 6 comments

Describe the bug I have one-to-many relationship tables. I want to store all my data in one form. Therefore, I created a nested form in parent controller. I have a number of images, which each image has caption text. In adding a new record, it inserts all inputs with image paths to the table fields. it means it saves all image paths in the database.

But the problem is when I go to edit action. After I submitted the edit form the image path(column name : image_url) value of child table goes to null.

class Flag extends Model
{
protected $table = 'flags';

protected $fillable =[
    'small_description',
    'cover_photo',
    'cover_photocaption',
];


public function flagimage()
{
    return $this->hasMany(Flagimage::class, 'flag_id', 'id');
}
}
class Flagimage extends Model
{
protected $table = 'flagimages';

protected $fillable = [
    'flag_id',
    'image_url',
    'image_caption',
];

public function flag()
{
    return $this->belongsTo(Flag::class, 'flag_id', 'id');
}
}
$form = new Form(new Flag());

        $form->select('language_id', __('Language id'))->options(Language::all()->pluck('language_name', 'id'))->rules('required');
        $form->select('location_id', __('Location id'))->options(Location::all()->pluck('name', 'id'))->rules('required');
        $form->textarea('small_description', __('Small description'));
        $form->image('cover_photo', __('Cover photo'))->move('flag/cover')->uniqueName();
        $form->textarea('cover_photocaption', __('Cover photocaption'));

        $form->table('flagimage', function (Form\NestedForm $form) {
            $form->image('image_url', __('Image'))->move('flag/images')->uniqueName();

            $form->textarea('image_caption', __('Caption'));
        });

Expected behavior how i work hasmany relation in form to handle image upload in both new record and update/edit record.

System

  • Open-admin version 1.0.27
  • PHP version 8.1
  • Laravel Version 9
  • OS: Windows
  • Browser chrome

kanishka55 avatar May 14 '23 15:05 kanishka55

@open-admin-org please consider this

kanishka55 avatar May 16 '23 02:05 kanishka55

@open-admin-org please check this.

kanishka55 avatar Jun 08 '23 10:06 kanishka55

I have the same issue.

For now I fixed it by using a select with values from a folder in storage. Maybe this works for your project too.

       ..., function (Form $form) {
                $files = collect(Storage::disk('public')->files('flags'));
                $options = [];
                foreach ($files as $file) {
                    $options[$file] = basename($file);
                }

                $form->table('locations', __('admin.locations'), function ($table) use ($options) {
                    $table->select('flag', __('admin.flag'))->options($options);
                    $table->text('description', __('admin.description'));
                    $table->text('value', __('admin.value'));
                });
            });

In combi with the Media manager extension this worked for my users.

bytebrain avatar Aug 05 '23 17:08 bytebrain

i will check this in my project. thank you @bytebrain

kanishka55 avatar Aug 15 '23 12:08 kanishka55

now fixed in dev-branch

open-admin-org avatar Nov 04 '23 23:11 open-admin-org

@kanishka55 I took a look at your example. It uses:

$form->table()

This it not working properly with relations. This is for the use with JSON fields. (Field now has an extra check for this)

$form->hasMay(...)->mode("table");

open-admin-org avatar Nov 04 '23 23:11 open-admin-org