Dumbed down upload for a form (points to file-ft on the stream)

edster - 2 weeks ago

I have a stream that I need to allow people to upload a document to.

Document shouldn't be uploaded or saved before the form passes validation.

How is the best way to tackle this? I can't just use the files ft, because - it uploads before the form goes anywhere or is validated. - if you do allow it to upload, it displays and edit or cancel button, even though its already been uploaded. - I need to force a naming standard for these files, or they will be overwriting each other all day long.

Answer

- 2 weeks ago

Try this out too: https://github.com/anomalylabs/upload-field_type

edster - 2 weeks ago

So I kinda sorta figured out a work around.

if I use

public function onSaving(Request $request, MountManager $manager, FolderRepositoryInterface $folders){
        if ($document = $request->file('document')){

            //The folder and disk this file should be saved in
            $folder = $folders->findBySlug('slug');
            $disk = $folder->getDisk();

            //The current user and time
            $user = $this->getFormEntry()->user;
            $time = Carbon::now('America/Vancouver');

            //We need to make sure the name is unique
            $file_name = $user->id.' - '.$user->display_name.' - Access Request - '.$time->format('Y-m-d His').'.pdf';

            //Place/Save the file
            $file = $manager->put($disk->getSlug() . '://' . $folder->getSlug() . '/' . $file_name, file_get_contents($document->getRealPath()));

            $this->getFormEntry()->file =  $file->id;
        }

    }

Then it will save the file then assign it to the form before form save.

However, this doesn't help with validation, I was hoping to be bale to just throw in a validator to add on, but that doesn't work. It also doesn't answer how do I actually get the input 'document' into the form.

I know if I had a basic file ft then I could handle the validation and other crap in there, but that is also giving me a headache.

Is there no easier way to handle this?

ryanthompson - 2 weeks ago

Try this out too: https://github.com/anomalylabs/upload-field_type

edster - 2 weeks ago

Ryan you are a gentleman and scholar! I wish I knew of that FT before as I was trying to replicate basically the same thing, just not as well lol.

For those curious this was my ending solution In protected $fields:

'document'  => [
            'required' => true,
            'label'    => 'Access Request',
            'instructions' => 'Upload a signed request .',
            'warning' => 'File must be a PDF',
            'type'     => 'anomaly.field_type.upload',
            'config'   => [
                'folder' => 'slug',
                'mimes' => ['pdf']
            ]
        ],

Then:

public function onSaving(Request $request, MountManager $manager, FolderRepositoryInterface $folders){
        if ($document = $request->file('document')){

            //The folder and disk this file should be saved in
            $folder = $folders->findBySlug('slug');
            $disk = $folder->getDisk();

            //The current user and time
            $user = $this->getFormEntry()->user;
            $time = Carbon::now('America/Vancouver');

            //We need to make sure the name is unique
            $file_name = $user->id.' - '.$user->display_name.' - Access Request - '.$time->format('Y-m-d His').'.pdf';

            //Place/Save the file
            $file = $manager->put($disk->getSlug() . '://' . $folder->getSlug() . '/' . $file_name, file_get_contents($document->getRealPath()));

            $this->getFormEntry()->file =  $file->id;
        }

    }

    public function onPosting(){

        $this->setSkips(['document']);
    }