Page 1 of 1

Requiring attachment based on other field

Posted: 22 Feb 2016
by hp
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;

};

Re: Requiring attachment based on other field

Posted: 22 Feb 2016
by rostislav
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

Re: Requiring attachment based on other field

Posted: 22 Feb 2016
by hp
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,

Re: Requiring attachment based on other field

Posted: 24 Feb 2016
by rostislav
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() != "" 

Re: Requiring attachment based on other field

Posted: 26 Feb 2016
by hp
I tested. that's not it. Thanks for trying. :)

Re: Requiring attachment based on other field

Posted: 26 Feb 2016
by hp
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.

Re: Requiring attachment based on other field

Posted: 29 Feb 2016
by rostislav
"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;
})