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
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.
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.
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;
}
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.
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?
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:
I'm quite new to plumsail (and also newbie coding) hopefully the screenshot makes sense.
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):
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;
}
}
});
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;
}
}
});
});