Requiring attachment based on other field

rss

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;

};

  • rostislav
    rostislav
    Moderator
    Member for: 4 years 3 months 12 days
    #1 by rostislav Feb 22 ’16 at 11:43

    The message was removed by a forum moderator.

  • rostislav
    rostislav
    Moderator
    Member for: 4 years 3 months 12 days
    #2 by rostislav Feb 22 ’16 at 11:50

    [#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

  • hp
    Member
    Member for: 4 years 9 months 6 days
    #3 by hp Feb 22 ’16 at 01:21

    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,

  • rostislav
    rostislav
    Moderator
    Member for: 4 years 3 months 12 days
    #4 by rostislav Feb 24 ’16 at 05:25

    [#3]:

    Probably because you have things like this there

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

    Whereas you probably mean

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

     

  • hp
    Member
    Member for: 4 years 9 months 6 days
    #5 by hp Feb 26 ’16 at 05:46

    I tested. that's not it. Thanks for trying. smile

  • hp
    Member
    Member for: 4 years 9 months 6 days
    #6 by hp Feb 26 ’16 at 06:55

    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.

  • rostislav
    rostislav
    Moderator
    Member for: 4 years 3 months 12 days
    #7 by rostislav Feb 29 ’16 at 05:59

    [#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;
    }) 
    
Displaying 1 to 7 of 7 messages