How to accept only images and allow empty field also

i want to allow a user to only upload images and also allow the field to be empty
so in the server action i set the allowed types to be .png,.jpg,.jpeg
but if the user did not upload any image the error message show about the file type.

I guess you can handle that with empty imagesize, NULL or „ „ to handle a fallback. But it looks like what you want is against the logic: Getting zero values and only picture filetypes.

it is logical
if the user upload i want it to be image.
dose this seems not logical?

you mean empty and also images?

yes that's what i mean

no one know? it seems a simple thing to do. and a must in some situations

It is not really Wappler related. You could prevent empty files with JS / Jquery I guess or handle all via Serverside:

if($_FILES["file"]["error"] == 4) {
//means there is no file uploaded
}

This just checks for empty file. You can add allowance for filetype by adding || ad the end of 4. Of course these lines needs to be changed to your needs

it is wappler related :sweat_smile:
lets say you have a portal and your users can upload their avatar image if they like or leave it empty.

this should be easily done in wappler. sure im missing something here

not really. Wappler gives you the power to do visually things easily which are commonly used. So you can develop stuff quickly. See philosophy. But I really guess you can only handle file type handling via GUI and maybe need to change one or two php code lines so it fits your requirements especially with empty files. Maybe you can use IF/ELSE in steps but if there is no „Filesize Empty“ parameter for upload, then youll need some php code to change. But as told: Maybe youre better here with a Jquery/JS prevention so a user cannot send empty stuff. Did you check Validation possibilities for jquery forms?

JQuery empty Fileupload prevention:

if ($('#videoUploadFile').get(0).files.length === 0) {
    console.log("No files selected.");
}

Some are automatically generated in Wappler.

solved it.
you cant set the validation on the POST field if you want to accept some file types and at the same time accept empty field if nothing is posted.

what you do is add a condition and you check the file name {{$_POST.pickName.name}}
then you add a validate data inside the condition to check the file type.

Great! Screenshot? So we could add this as Howto & Solved

sure

1- add condition

2- add validate data inside the condition

4 Likes

I have created the same for the AppConnect in DW some time ago. Passing you a link of the notes I kept back then, in case you find them interesting. In my case you also resize the image uploaded. Thanks
https://www.evernote.com/shard/s593/sh/bb89fcc5-c709-4a70-99cb-09dc549e54fe/a35e47d37c61d663d823494adc02f3eb

2 Likes