Is there a way to add multiple checks in liveview allow_upload?

Hi,

Greetings to everyone. I hope you all are having a wonderful day/night.

I am working on a live view app and have a requirement where the user can upload only one video OR can upload up to 5 photos. I have the live view as follows

@extension_whitelist ~w(.jpg .jpeg .png .mp4)

allow_upload(:photo_urls,
accept: @extension_whitelist,
max_entries: 5,
max_file_size: 30_000_000)}

So if the extension is .mp4, i should allow max_entries = 1 and everything else ( i.e, .jpg .jpeg .png ) should be up to 5 entries.

Appreciate your help.

Regards,
Venkat

For your use case I would recommend making two upload configs with the desired settings- for example one named video and one named images, and then disabling one when the other is in use. You will need to render two live_file_input elements but it should be much simpler to validate your constraints this way.

If you must use a single input, then you will need to set max_entries to 5 and then use the low-level uploaded_entries/2 function to manage the entries and validate your specific conditions, like cancelling entries when a video is selected.

2 Likes