Is there a way to require that a person attach an item to the form?
Thanks
Jennifer
Hello @jktodd007,
You can add a custom validator to the form. So when a user submits the form and no files attached, he will receive an error message.
Please see the code sample below.
fd.spRendered(function(){
fd.field('Attachments').validators.push({
name: 'Attachments Validation',
error: "Please add attachment",
validate: function() {
if (fd.field('Attachments').value.length == 0) {
return false;
}
return true;
}
})
});
I tried the code as you have it here as well as putting in a Before save and it did not work
Here is my code
fd.spBeforeSave() {
fd.field('Attachments').validators.push({
name: 'Attachments Validation',
error: "Please add attachment",
validate: function() {
if (fd.field('Attachments').value.length == 0) {
return true;
}
return false;
}
})
var str = fd.field('EmployeeID').value;
var res = str.toUpperCase();
fd.field('EmployeeID').value = res;
});
It should be used under fd.spRendered event handler. It is my mistake in the code, I'm sorry.
Please see below. the correct one
fd.spRendered(function(){
fd.field('Attachments').validators.push({
name: 'Attachments Validation',
error: "Please add attachment",
validate: function() {
if (fd.field('Attachments').value.length == 0) {
return false;
}
return true;
}
})
});
Hi! I'm trying to validate attachments using boolean fields. Here is my code and it seems to be not working. What am I doing wrong here? Thank you!
fd.spRendered(function(){
fd.field('Attachments').ready().then(function() {
fd.field('Attachments').validators.push({
name: 'Attachments Validation',
error: "ATTACHMENTS REQUIRED. PLEASE ATTACH YOUR REQUEST FORM OR COMPLETION CARD",
validate: function() {
if (fd.field('Complete').value === true || fd.field('NotFullyComplete').value === true || fd.field('ExemptionRequest').value === true) {
return false;
}
return true;
}
});
});
});
The attachment is required when Complete field is "Yes", or Not Fully Complete field is "Yes", or Exempt Requested is "Yes".
Hello @Jamail_Serio,
You need to add the condition to check if files are uploaded or not:
fd.spRendered(function(){
fd.field('Attachments').ready().then(function() {
fd.field('Attachments').validators.push({
name: 'Attachments Validation',
error: "ATTACHMENTS REQUIRED. PLEASE ATTACH YOUR REQUEST FORM OR COMPLETION CARD",
validate: function(value) {
//files are not uploaded
if(value.length == 0){
if (fd.field('Complete').value === true || fd.field('NotFullyComplete').value === true || fd.field('ExemptionRequest').value === true) {
return false;
}
return true;
}
}
});
});
});
Hi @mnikitina !
I hope you're well and thank you for the help always.
The code works. The only issue I have now is that it triggers all the time. My form has four options.
- Complete - Attachment is required
- Not Fully Complete - attachment is required
- Exemption Request - attachment is required
- Not Complete - attachment is NOT required.
When a user chooses "Not Complete", then attachment is not required. I added an extra condition and it doesn't seem to work
else {
if (fd.field('NotComplete').value === true) {
return true;
}
}
}
}
});
});
Hello @Jamail_Serio,
Try out this code:
fd.field('Attachments').ready().then(function() {
fd.field('Attachments').validators.push({
name: 'Attachments Validation',
error: "ATTACHMENTS REQUIRED. PLEASE ATTACH YOUR REQUEST FORM OR COMPLETION CARD",
validate: function(value) {
//files are not uploaded
if(value.length == 0){
if (fd.field('NotComplete').value === true) {
return true;
}
if (fd.field('Complete').value === true || fd.field('NotFullyComplete').value === true || fd.field('ExemptionRequest').value === true) {
return false;
}
}
return true;
}
});
});