Comments (5)
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');
});
from arcanist.
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
from arcanist.
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:
$payload['name']
is correctly validated and saved;- while
$payload['avatar']
is just an empty array, which of course generates exception afterStorage::putFile
Call to a member function hashName() on array
from arcanist.
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']
.
from arcanist.
Thanks a lot! This is what i suspected))
from arcanist.
Related Issues (20)
- It is not possible to access shared data in the step class HOT 1
- Inertia support HOT 7
- REST API Renderer HOT 1
- [REST API Renderer] Wizard Cancel force redirect Response HOT 3
- Overriding registerRoutes (for multilingual support, etc.) HOT 2
- Question about security HOT 7
- How should 404 errors be handled HOT 1
- Use the var that passed from action [question]
- A prepareForValidation method HOT 1
- Repeat option HOT 1
- laravel-arcanist/inertia-response-renderer 0.5.0 requires inertiajs/inertia-laravel ^0.4.1 -> found inertiajs/inertia-laravel[v0.4.1, ..., v0.4.5] but it conflicts with your root composer.json require (^0.5.1).
- Access level to fields() must be public HOT 1
- Is version 1.0 accessible?
- Optional steps HOT 1
- How to validate an array of objects in wizard step HOT 1
- UnknownStepException
- Testing HOT 6
- arc lint doesnt see all the paths in the directory HOT 2
- Can we also contribute to the documentation somewhere? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from arcanist.