Attachments Field Is Empty

Hello,

I am looking to force people to add an attachment to the form. It doesn't look like I can make the attachments field a required field, so is there a way for me to hide the submit button based on the attachments being empty?

I have been able to hide buttons based on text fields, but have not figured out the syntax for checking to see if the Attachments field is empty or not.

Thanks in advance for your help.

Hello @wbrunner,

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;
    }
})

});

Hello @mnikitina,
I use above code for validation and it is working fine,
But when i successfully added Attachments it not removing error like other fields,
Like number or date and so on fields.
Please go through below image,

1> Try to save without validation fields

2>After enter value in fields, it error message is gone without clicking save button.

Problem is attachments fields are not behaving like other fields, even if attach documents it shows error message continuously, and allowing me to save.

code i am useing is below,

fd.field('Attachments').validators.push({

                    name: 'Attachment validator',

                    error: 'Please provide contract Attachment.',

                    validate: function () {

                        if (fd.field('Attachments').value.length == 0) {

                            var attachmenterror = filtertooltip(tooltiparr, "Contract Attachment", "Required");

                            this.error = !isNullorUndefined(attachmenterror) ? attachmenterror.Hilfstext : "Please provide contract Attachment.";

                            return false;

                        }

                        return true;

                    }

                });

Thanks in advance.

Hello @harshp924,

Developers confirmed that this is a bug. I will let you know once it is fixed.

Thanks @mnikitina for looking into this matter!

1 Like

Hi,

I used the code and it was working. Then today, I got this error message.

Cannot read property 'length' of undefined

Thanks.
Jamail

Hello @Jamail_Serio,

If there were no changes in the form, probably the field was not fully loaded, and the code threw the error. Please try to run code under the ready event.

fd.spRendered(function(){
        fd.field('Attachments').ready().then(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;
                    }
                });
        });
});

If there were any changes, please double-check the internal name of the field.

Hi @mnikitina,

I used the code above and the error still remains under the field.

It appears in this scenario:

Before I attach the file to the field, I hit the step-change (Next) button to check if all the fields are validated. After I attach the file to the field, the error remains below the field.

Here is the code I am using:

//This code will validate the New Name field - [Personal Details]
fd.field('Attachments').ready().then(function() {
                fd.field('Attachments').validators.push({
                        name: 'Attachments Validation',
                        error: "Please add attachment",
                        validate: function() {
                            if (fd.field('Question_2').value.indexOf('Personal Details')>=0
                            && fd.field('Attachments').value.length == 0) {
                                    return false;
                                }
                            return true;
                    }
                });
});

Hello @DryChips,

Thank you for reporting this! Developers confirmed that this is a bug.

As a temporary workaround you can add this code alongside with the field validation:

fd.field('Attachments').$on('change', function(value) {
    fd.field('Attachments').validate()
});
1 Like

Thank you!

Please do let me know when the bug is fixed. :slightly_smiling_face: