Attachment Validation

Hello,

How can i only let users upload .pdf files? I have tried 'Allowed Ext.' which gives the message File type not allowed but allows a user to still click save.

How can i disable the save button until the correct extension has been added?

Thank you

Hello @jamesmitchell,

You need to make the field required to make sure users uploaded the correct file to the attachments before submitting the form:
image

Thank you! I didnt see that.

It works now

sorry, i actually need to do the validation on the sharepoint attachment.

I need to be able to only allow users to upload and submit .pdf files.

Is it possible to do this validation on the sharpeoint attachment field?

@jamesmitchell,

For SharePoint Attachments field you need to add custom validation with the code:

fd.field('Attachments').addValidator({
	name: 'Attachments Validation',
	error: "",
	validate: function(value) {
		if (value.length == 0) {
			this.error = "Please add attachment"
			return false;
		} else {
			for (let i = 0; i < value.length; i++) {
				if (value[i].extension != '.pdf') {
					this.error = "Only PDF files are allowed"
					return false;
				}
			}
		}
		return true;
	}
});

Hello,

Thank you for this. I have tried it but am getting the error [SharePoint Forms] Error in custom JS:TypeError: Cannot read properties of null (reading 'validators')

Thank you

@jamesmitchell,

I'm sorry, my bad. It must be addValidator not validators.push. Please test this code:

fd.field('Attachments').addValidator({
	name: 'Attachments Validation',
	error: "",
	validate: function(value) {
		if (value.length == 0) {
			this.error = "Please add attachment"
			return false;
		} else {
			for (let i = 0; i < value.length; i++) {
				if (value[i].extension != '.pdf') {
					this.error = "Only PDF files are allowed"
					return false;
				}
			}
		}
		return true;
	}
});

Sorry, now it gives the error:

[SharePoint Forms] Error in custom JS:TypeError: Cannot read properties of null (reading 'addValidator')

@jamesmitchell,

Please make sure you are using the correct internal name of the field in the code:

image

Also, try wrapping the code inside the ready() event like so:

fd.field('Attachments').ready().then(function(field) {
	fd.field('Attachments').addValidator({
		name: 'Attachments Validation',
		error: "",
		validate: function(value) {
			if (value.length == 0) {
				this.error = "Please add attachment"
				return false;
			} else {
				for (let i = 0; i < value.length; i++) {
					if (value[i].extension != '.pdf') {
						this.error = "Only PDF files are allowed"
						return false;
					}
				}
			}
			return true;
		}
	});
});

Sorry, now it says

[SharePoint Forms] Error in custom JS:TypeError: Cannot read properties of null (reading 'ready')

The field is definitely called Attachments

@jamesmitchell,

Please export the form and share it with me. I'll review and test it.