Pre-fill support for file input (#222)

Co-authored-by: Julien Nahum <julien@nahum.net>
This commit is contained in:
formsdev
2023-10-20 14:30:35 +05:30
committed by GitHub
parent c470791282
commit 4614dc0f18
6 changed files with 63 additions and 17 deletions

View File

@@ -16,15 +16,20 @@ class UploadAssetRequest extends FormRequest
*/
public function rules()
{
$fileTypes = [
'png',
'jpeg',
'jpg',
'bmp',
'gif',
'svg'
];
if ($this->offsetExists('type') && $this->get('type') === 'files') {
$fileTypes = [];
}
return [
'url' => ['required',new StorageFile(self::FORM_ASSET_MAX_SIZE, [
'png',
'jpeg',
'jpg',
'bmp',
'gif',
'svg'
])],
'url' => ['required', new StorageFile(self::FORM_ASSET_MAX_SIZE, $fileTypes)],
];
}
}

View File

@@ -46,7 +46,9 @@ class FormSubmissionResource extends JsonResource
});
foreach ($fileFields as $field) {
if (isset($data[$field['id']]) && !empty($data[$field['id']])) {
$data[$field['id']] = collect($data[$field['id']])->map(function ($file) {
$data[$field['id']] = collect($data[$field['id']])->filter(function ($file) {
return $file !== null && $file;
})->map(function ($file) {
return [
'file_url' => route('open.forms.submissions.file', [$this->form_id, $file]),
'file_name' => $file,

View File

@@ -4,6 +4,7 @@ namespace App\Jobs\Form;
use App\Events\Forms\FormSubmitted;
use App\Http\Controllers\Forms\PublicFormController;
use App\Http\Controllers\Forms\FormController;
use App\Http\Requests\AnswerFormRequest;
use App\Models\Forms\Form;
use App\Models\Forms\FormSubmission;
@@ -162,6 +163,14 @@ class StoreFormSubmissionJob implements ShouldQueue
return null;
}
if(filter_var($value, FILTER_VALIDATE_URL) !== FALSE && str_contains($value, parse_url(config('app.url'))['host'])) { // In case of prefill we have full url so convert to s3
$fileName = basename($value);
$path = FormController::ASSETS_UPLOAD_PATH . '/' . $fileName;
$newPath = Str::of(PublicFormController::FILE_UPLOAD_PATH)->replace('?', $this->form->id);
Storage::move($path, $newPath.'/'.$fileName);
return $fileName;
}
if($this->isSkipForUpload($value)) {
return $value;
}

View File

@@ -40,6 +40,11 @@ class StorageFile implements Rule
*/
public function passes($attribute, $value): bool
{
// If full path then no need to validate
if (filter_var($value, FILTER_VALIDATE_URL) !== FALSE) {
return true;
}
// This is use when updating a record, and file uploads aren't changed.
if($this->form){
$newPath = Str::of(PublicFormController::FILE_UPLOAD_PATH)->replace('?', $this->form->id);