Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to upload files? #39

Open
madi-nuralin opened this issue Jan 24, 2022 · 5 comments
Open

How to upload files? #39

madi-nuralin opened this issue Jan 24, 2022 · 5 comments

Comments

@madi-nuralin
Copy link

madi-nuralin commented Jan 24, 2022

Hi,

I'm experiencing with this package and I have a trouble with file uploading:

  • In WizardAction I'm reading a file field (e.g. avatar) through $payload and i want to do something like Storage::putFile('avatars', $payload['avatar']); to store file in a local storage, but $payload['avatar'] is always an empty array. How to fix that?
@matthewmnewman
Copy link

matthewmnewman commented Jan 24, 2022

Hey,

This is more than likely related to you not correctly defining your avatar field within one of your wizard steps.

Fields() - In this method you should define the fields that exist on this step of the form as well as any validation rules that go along with them. In other words, this is how Arcanist knows what data it should save when the step gets submitted.

At the end of each step, Arcanist will save the data if defined within your steps and you will then be able to access this via the $payload variable on the WizardAction.

Provide me with your avatar upload step so I can assist you more.

Thanks

@madi-nuralin
Copy link
Author

madi-nuralin commented Jan 24, 2022

Thanks for your reply!

I'm working with Inertia.js.

  • The uploading step looks like this:
class ProfileInformationStep extends WizardStep
  public string $title = 'Profile Information';
  public string $slug = 'profile-information';

  public function viewData(Request $request): array
  {
          return $this->withFormData();
  }

  public function fields(): array
  {
          return [
              Field::make('name')
                  ->rules(['required', 'string']),
              Field::make('avatar')
                  ->rules(['required', 'mimes:png,jpg,jpeg']),
          ];
  }
}
  • View
<template>
<input type="text" v-model="form.name" />
<input type="file" @input="form.avatar = $event.target.files[0]" />
</template>
data() {
    return {
        form: this.$inertia.form({
            name: null,
            avatar: null,
        }),
    }
},

methods: {
      submit() {
          this.form.post(this.url);
      },
  },
  • Action class:
class SubmissionAction extends WizardAction
{
    public function execute($payload): ActionResult
    {
         $user = auth()->user();
         $user->name = $payload['name'];
         Storage::putFile('avatars', $payload['avatar']);
         // ....
    }
}

Note:

  1. $payload['name'] is correctly validated and saved;
  2. while $payload['avatar'] is just an empty array, which of course generates exception after Storage::putFile
Call to a member function hashName() on array

@ksassnowski
Copy link
Collaborator

Arcanist doesn’t natively understand how to deal with file uploads. What you want to do in this case is define a transformer for the avatar field and save the file there. Whatever the transform callback returns is what gets saved as part of the wizard’s data.

Field::make('avatar')
    ->rules(['required', 'file', 'mimes:png,jpg,jpeg'])
    ->transform(function (UploadedFile $file) {
        return $file->store('avatars');
    });

Note that this means that the file gets stored when the step gets submitted, not when the wizard is finished. In your action, you would then be able to grab the filename via $payload['avatar'].

@madi-nuralin
Copy link
Author

madi-nuralin commented Jan 24, 2022

Thanks a lot! This is what i suspected))

@madi-nuralin
Copy link
Author

madi-nuralin commented Feb 22, 2022

Hi,
It's me again, I'm reopening the issue with the following question:
Continuing the topic of loading an avatar: if I need to upload an array of avatars then how to validate array of files?

It seems like it is not enogh to put .* at Field::make('avatars.*')

Field::make('avatars.*')
    ->rules(['required', 'file', 'mimes:png,jpg,jpeg'])
    ->transform(function (UploadedFile $file) {
        return $file->store('avatars');
    });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants