Requiring attachment based on other field
Posted by hp - Feb 22 ’16 at 11:07
Hello,
i am trying to require attachment on save button if name or address fields are filled out. could you please advice how to accomplish this? with my code it checks for attachment regardless of what values are filled out it sends out alert to attach an attachment. Please advise. Thanks in advance!
if ((fd.field('Name').value() != " ") && ($('#idAttachmentsTable tr').length == 0)){
if ((fd.field('Mailing').value() != " ") && ($('#idAttachmentsTable tr').length == 0))
{
alert('Please attach an attachment as supporting document');
return false;
}
return true;
};
-
rostislavModeratorMember for: 7 years 1 month 26 days
The message was removed by a forum moderator.
-
rostislavModeratorMember for: 7 years 1 month 26 days
[#1]:
Hi!
Try this:
fd.onsubmit(function(){ if ((fd.field('Name').value() || fd.field('Mailing').value()) && fd.field('Attachments').control()._el().find('#idAttachmentsTable tr').length == 0) { alert('Attachments is a required field!'); return false; } return true; })
However, I don't know what Name and Mailing fields' types are, take a look here at the various field types and corresponding ways of getting their values: http://formsdesigner.blogspot.com/2013/04/getting-and-setting-sharepoint-form.html
-
hpMemberMember for: 7 years 7 months 21 days
Thanks that works. But iwas wondering why did nested if didn't work as i am trying to use it in different scenario where based on which field is empty different alerts would pop up for user.
Thanks,
-
rostislavModeratorMember for: 7 years 1 month 26 days
[#3]:
Probably because you have things like this there
fd.field('Mailing').value() != " "
Whereas you probably mean
fd.field('Mailing').value() != ""
-
hpMemberMember for: 7 years 7 months 21 days
I tested. that's not it. Thanks for trying.
-
hpMemberMember for: 7 years 7 months 21 days
As well validations always returns false when i use your method for 3 fields. for example name,address and phone. If any of the field has value attachment can't be blank.
-
rostislavModeratorMember for: 7 years 1 month 26 days
[#6]:
"As well validations always returns false when i use your method for 3 fields. for example name,address and phone. If any of the field has value attachment can't be blank."
Show us the code and tell us how you expect it to work.
Regarding the code you posted initially what you are saying with it is:
if name is not one empty space and there are no Attachments
and mailing is not one empty space and there are no Attachments
in that case alert and return false
otherwise return true
I presume that's not what you want. If you want to display different alert messages based on different fields being empty or not, you should do:
fd.onsubmit(function(){ if (fd.field('Name').value() && fd.field('Attachments').control()._el().find('#idAttachmentsTable tr').length == 0) { alert('alert 1'); return false; } if (fd.field('Mailing').value() && fd.field('Attachments').control()._el().find('#idAttachmentsTable tr').length == 0){ alert('alert 2'); return false; } return true; })