Only allow clipboard pasting, not typing, into a field.

I have a field where users should be pasting in content from another tool, and not directly entering information into a Textarea. Is there a way to control a field so the user cannot type into the Textarea, but can paste into it. Or is there a better method than using a Textarea to let the user throw stuff from the clipboard into the form? OR Is it possible to use a fileUpload, but instead of uploading the file to my storage, it just pulls the content of the (xml) file and fills in a readOnly field?
Solution:
You would use the closure Get and Sets ```php ->afterStateUpdated(function($state, $get, $set) { $state = your file // do processing...
Jump to solution
6 Replies
MarconiMamba
MarconiMamba9mo ago
thinking about it more I much strongly prefer the file upload method if it's possible.
toeknee
toeknee9mo ago
You can allow file upload, then afterStateUpated process the file and populate it's content into the form. However, when you have disabled fields you cannot save them natively as we remove them. You'll need to do some custom work to make this work
MarconiMamba
MarconiMamba9mo ago
I've used ->readOnly with fields to make them not editable but still insert into the DB fine. What do I pass into the function on afterStateUpdated on the file to get the contents? Does $state contain the file?
Solution
toeknee
toeknee9mo ago
You would use the closure Get and Sets
->afterStateUpdated(function($state, $get, $set) {
$state = your file // do processing
$fileData = Processes

$set('my_field', $fileData);

})
->afterStateUpdated(function($state, $get, $set) {
$state = your file // do processing
$fileData = Processes

$set('my_field', $fileData);

})
MarconiMamba
MarconiMamba9mo ago
Last question for now. If I don't want the file itself stored or put into the database, is there anything special I need to do? Or if I just have the FileUpload::make('nonexistent_field') will that work? I'm having a lot of trouble getting the content of the File i'm uploading. I've tried
$xml = simplexml_load_string(file_get_contents($state));
-- file_get_contents(C:\[obfuscated]\Temp\php96E.tmp): Failed to open stream: No such file or directory
$xml = simplexml_load_string(file_get_contents($state));
-- file_get_contents(C:\[obfuscated]\Temp\php96E.tmp): Failed to open stream: No such file or directory
$xml = simplexml_load_string(file_get_contents($state->realPath));
-- Undefined property: Livewire\Features\SupportFileUploads\TemporaryUploadedFile::$realPath
$xml = simplexml_load_string(file_get_contents($state->realPath));
-- Undefined property: Livewire\Features\SupportFileUploads\TemporaryUploadedFile::$realPath
$xml = simplexml_load_string(file_get_contents($state->file->getFilename()));
$xml = simplexml_load_string(file_get_contents($state->file->getFilename()));
$xml = simplexml_load_string(file_get_contents($state->file->getFilename()));
$xml = simplexml_load_string(file_get_contents($state->file->getFilename()));
both with similar results the ddd of $state shows me the actual path for the file as realPath
Livewire\Features\SupportFileUploads\TemporaryUploadedFile {#2038 ▼ // vendor\spatie\laravel-ignition\src\helpers.php:14
-test: false
-originalName: "Hqwv9i9GqU19a61jnEueuUnsSbxoxF-metaQVRNRU5VLnVudXNlZFRpY2tldE92ZXJyaWRlLnhtbA==-.txt"
-mimeType: "application/octet-stream"
-error: 0
#hashName: null
#disk: "local"
#storage: Illuminate\Filesystem\FilesystemAdapter {#2028 ▶}
#path: "livewire-tmp/Hqwv9i9GqU19a61jnEueuUnsSbxoxF-metaQVRNRU5VLnVudXNlZFRpY2tldE92ZXJyaWRlLnhtbA==-.txt"
path: "D:\[obfuscated]\storage\app\livewire-tmp"
filename: "Hqwv9i9GqU19a61jnEueuUnsSbxoxF-metaQVRNRU5VLnVudXNlZFRpY2tldE92ZXJyaWRlLnhtbA==-.txt"
basename: "php7F25.tmp"
pathname: "C:\[obfuscated\Temp\php7F25.tmp"
extension: "tmp"
realPath: "D:[obfuscated]\storage\app\livewire-tmp/Hqwv9i9GqU19a61jnEueuUnsSbxoxF-metaQVRNRU5VLnVudXNlZFRpY2tldE92ZXJyaWRlLnhtbA==-.txt"
aTime: 2023-11-02 21:24:08
mTime: 2023-11-02 21:24:08
cTime: 2023-11-02 21:24:08
inode: 737182964005292854
size: 154
writable: false
readable: false
executable: false
file: false
dir: false
link: false
}
Livewire\Features\SupportFileUploads\TemporaryUploadedFile {#2038 ▼ // vendor\spatie\laravel-ignition\src\helpers.php:14
-test: false
-originalName: "Hqwv9i9GqU19a61jnEueuUnsSbxoxF-metaQVRNRU5VLnVudXNlZFRpY2tldE92ZXJyaWRlLnhtbA==-.txt"
-mimeType: "application/octet-stream"
-error: 0
#hashName: null
#disk: "local"
#storage: Illuminate\Filesystem\FilesystemAdapter {#2028 ▶}
#path: "livewire-tmp/Hqwv9i9GqU19a61jnEueuUnsSbxoxF-metaQVRNRU5VLnVudXNlZFRpY2tldE92ZXJyaWRlLnhtbA==-.txt"
path: "D:\[obfuscated]\storage\app\livewire-tmp"
filename: "Hqwv9i9GqU19a61jnEueuUnsSbxoxF-metaQVRNRU5VLnVudXNlZFRpY2tldE92ZXJyaWRlLnhtbA==-.txt"
basename: "php7F25.tmp"
pathname: "C:\[obfuscated\Temp\php7F25.tmp"
extension: "tmp"
realPath: "D:[obfuscated]\storage\app\livewire-tmp/Hqwv9i9GqU19a61jnEueuUnsSbxoxF-metaQVRNRU5VLnVudXNlZFRpY2tldE92ZXJyaWRlLnhtbA==-.txt"
aTime: 2023-11-02 21:24:08
mTime: 2023-11-02 21:24:08
cTime: 2023-11-02 21:24:08
inode: 737182964005292854
size: 154
writable: false
readable: false
executable: false
file: false
dir: false
link: false
}
awcodes
awcodes9mo ago
Try $state->getPath() Or $state->getRealPath() if you need the full path.