Restricting number of attachments to 1

I notice that adding an attachments field on a public form defaults to allow multiple attachments. I'd like to restrict this to one only. Is that possible? I tried a custom validation (below) but it wasn't successful.
Any hints very welcome

fd.rendered(function() {
fd.validators.push({
name: 'Check Attachment',
error: "Error text",
validate: function(value) {
if (fd.field('Attachment').value[1].size > '0')
return false;
return true;
}
});
});

Hello @duncanw,

If you want to know the number of uploaded files, you need to use this code:
fd.field('Attachments0').value.length

And to validate that a user uploaded only one file, please use the below code. Please make sure that you are using the internal name of the field in the code. You can find it in Field Settings >> InternalName.

image

fd.rendered(function(){  

    fd.validators.push({
        name: 'Check Attachment',
        error: "Error text",
        validate: function(value) {
            if (fd.field('Attachments0').value.length > 1) {
                return false;
            }
            else {
                return true;
            }
        }
    });

});
1 Like

So on the surface the below looks similar to what you've written but it doesn't run. The main difference is in the line
"validate: function(value) {"
What should go where the word "value" exists? I tried blank (below) and also the name of the field but neither helped.
Any tips much appreciated.

fd.rendered(function() {

//Validate advice isn't too long
            fd.validators.push({
	            name: 'InstructionsLengthValidator',
	            error: "Instructions must be not longer than 6000 characters",
	            validate: function() {
	            if (fd.field('ShortAdvice').value.length <= 6000) {
	                    return true; 
					}
	                else {
	                    return false;
	                }
				}
	        });

//Validate instructions aren't too long
            fd.validators.push({
	            name: 'InstructionsLengthValidator',
	            error: "Instructions must be not longer than 600 characters",
	            validate: function() {
	            if (fd.field('Instructions').value.length <= 600) {
	                        return true; 
					}
	                else {
	                        return false;
	                }
				}
	        });

//Validate there's at most one attachment
			fd.validators.push({
				name: 'Check Attachment',
				error: "Please attach no more than one attachment",
				validate: function() {
				if (fd.field('Attachment').value.length > 1) {
						return false; 
					}
					else {
						return true; 
					}
				}
            });
});

Think I've worked out what's going on here. If you have a validator saying "Must be less than 6000 characters" then I'm expecting that an empty submission should be fine since that is less than 6000 characters. But in fact <= 6000 means 1 >= x <=6000. That's annoying because need either 6000 characters in one field or one attachment in another, or neither, but never both. Need to work out a way to achieve that. I'll revert if I find a way.

Hello @duncanw,

Are ShortAdvice and Instructions multiple-line text fields?

You can check if the text length exceeds the required number of characters, that will allow users to submit the form if the field is blank.

//Validate advice isn't too long
            fd.validators.push({
	            name: 'InstructionsLengthValidator',
	            error: "Instructions must be not longer than 6000 characters",
	            validate: function() {
	            if (fd.field('ShortAdvice').value.length > 6000) {
	                    return false; 
					}
	                else {
	                    return true;
	                }

@mnikitina

Is there a way to restrict a list/library control upload functionality to only 1 file?

Thanks,
stormanh

Hello @stormanh,

You can add custom validation on the form that chacks the count of records in List or Library control.

fd.spRendered(function() {
	
	fd.validators;

	fd.validators.push({
		name: 'SPDataTable validator',
	    error: 'Error message',
	    validate: function() {
	        if (fd.control("SPDataTable0").widget.dataItems().length > 1) {
	            this.error = "Error message";
	            return false;
	        }

	        return true;
	    }
	});

});

Hi @mnikitina.

The validation for the form works when trying to save but I would like to prevent users from uploading more than one file to begin with as I have post processing being done on the uploaded file which I need to limit to just one file being uploaded. That is, I would like to limit the selection of the files to only 1 as shown below when the "Open" button is clicked where the user chooses what file to upload.

Is there any way to do that?

Hello @stormanh,

I apologize for the delayed reply.

I've clarified this with our developers team. It is currently not possible to restrict the number of attachments a user can upload at a time.

Would you consider a paid support for this? Please email us at support@plumsail.com to know the details.

Hi @mnikitina,

I am now using this code in my public form. I still do not get to limit the number of attachments to 1 (the form still allows users to upload multiple files). It seems the code is not affecting the attachments field. Can you please give me a hand to solve this?

Thanks in advance

Hello @andrescaicedo,

Welcome to Plumsail Community!

I suppose there is an error occuring before the validation is added. Please check the browser console (F12) for errors and share the screenshot.

Also please share the complete code you are using on the form as a text.

1 Like

Hi @mnikitina thanks for your reply.

Please see below the full code I'm using in the JS editor. The first 2 blocks of code are working fine but the last part isn't (attachment validation):

fd.rendered(function() {

function ShowActionTaken() {

    if (fd.field('choice1').value == 'Yes') {
        $(fd.field('text9').$parent.$el).show();
    } else {
        $(fd.field('text9').$parent.$el).hide();
    }
}
fd.field('choice1').$on('change',ShowActionTaken);

ShowActionTaken();
});

fd.rendered(function() {

    function settext9Required() {
        if (fd.field('choice1').value == 'Yes') {
            
            fd.field('text9').required = true;
        } else {
            
            fd.field('text9').required = false;
        }
    }

   
    fd.field('choice1').$on('change',settext9Required);

   
    settext9();

});



fd.rendered(function(){  

    fd.validators.push({
        name: 'Check Attachment',
        error: "Only One File is Allowed Please Remove Other Attachments",
        validate: function(value) {
            if (fd.field('Attachments1').value.length > 1 ) {
                return false;
            }
            else {
                return true;
            }
        }
    });

});

This is what I got from the browser console:

image

I'm quite new to plumsail (and also newbie coding) hopefully the screenshot makes sense.

Thanks in advance!

@andrescaicedo,

Thank you for the code!

You are calling the settext9() function that doesn't exist:

Also it is best to place all code within one rendered() event. Please see the updated code:

fd.rendered(function() {

    function ShowActionTaken() {

        if (fd.field('choice1').value == 'Yes') {
            $(fd.field('text9').$parent.$el).show();
        } else {
            $(fd.field('text9').$parent.$el).hide();
        }
    }
    fd.field('choice1').$on('change',ShowActionTaken);

    ShowActionTaken();

    function settext9Required() {
        if (fd.field('choice1').value == 'Yes') {
            
            fd.field('text9').required = true;
        } else {
            
            fd.field('text9').required = false;
        }
    }

   
    fd.field('choice1').$on('change',settext9Required);

   
    settext9Required();


    fd.validators.push({
        name: 'Check Attachment',
        error: "Only One File is Allowed Please Remove Other Attachments",
        validate: function(value) {
            if (fd.field('Attachments1').value.length > 1 ) {
                return false;
            }
            else {
                return true;
            }
        }
    });

});
1 Like

Hi @mnikitina ,

I updated the code, but the form is still allowing to upload several documents (more than one).

I also checked the browser console and I still get the same error (related to settext9):

image

For testing purposes I removed the first part of the code from the editor and left only the attachment validation part and it is not working either (the form is still allowing to upload more that 1 file):

fd.rendered(function() {

fd.validators.push({
    name: 'Check Attachment',
    error: "Only One File is Allowed Please Remove Other Attachments",
    validate: function(value) {
        if (fd.field('Attachments1').value.length > 1 ) {
            return false;
        }
        else {
            return true;
        }
    }
});

});

Note that when using only the attachment validation code the browser console does not return any error:

Sorry for giving you hard times but I ccould not find to make it work on my own.

Thanks again.

Hello @andrescaicedo,

No problem at all, I'm here to help!

Could you please export the form and share it here.

For this go to form Settings (gear icon) and click Export:
image

1 Like

Hi @mnikitina ,

Sure! Please see attached the .json file for my form.

Hello @andrescaicedo,

Validation works, but user will receive the error only on the last step. You can add validation to the Attachments field, not to the form, thus user will get the error when uploading more than one file.

fd.rendered(function() {
    fd.field('Attachments1').validators.push({
        name: 'Check Attachment',
        error: "Only One File is Allowed Please Remove Other Attachments",
        validate: function(value) {
            if (fd.field('Attachments1').value.length > 1 ) {
                return false;
            }
            else {
                return true;
            }
        }
    });
});
1 Like

Hi @mnikitina,

I updated this line fd.field('Attachments1').validators.push({ instead of fd.validators.push({ in the JS editor and now the form works brilliantly.

Thank you heaps for your help. Great support!

1 Like