Requiring attachment based on other field

Discussions about Forms Designer for SharePoint 2013 / 2016 and Office 365.
Locked
hp
Posts: 26
Joined: Thu Jan 15, 2015

22 Feb 2016

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;

};

User avatar
rostislav
Moderator
Posts: 364
Joined: Mon Oct 19, 2015

22 Feb 2016

Hi!

Try this:

Code: Select all

 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://spform.com/forms-designer- ... eld-values

hp
Posts: 26
Joined: Thu Jan 15, 2015

22 Feb 2016

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,

User avatar
rostislav
Moderator
Posts: 364
Joined: Mon Oct 19, 2015

24 Feb 2016

Probably because you have things like this there

Code: Select all

fd.field('Mailing').value() != " "
Whereas you probably mean

Code: Select all

fd.field('Mailing').value() != "" 

hp
Posts: 26
Joined: Thu Jan 15, 2015

26 Feb 2016

I tested. that's not it. Thanks for trying. :)

hp
Posts: 26
Joined: Thu Jan 15, 2015

26 Feb 2016

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.

User avatar
rostislav
Moderator
Posts: 364
Joined: Mon Oct 19, 2015

29 Feb 2016

"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:

Code: Select all

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

Locked
  • Information
  • Who is online

    Users browsing this forum: No registered users and 15 guests