locked
Preventing infopath from caling server side code if applied rules failed RRS feed

  • Question

  • Hi all, I am new to InfoPath forms. I am trying to add validation to the text box. For this I add rule to the button for the textbox lets say 'txtName'

    Condition: txtName is is blank

    Actoin: Show dialog box message

     

    but when I click the button after executing this rule InfoPath runs server side code as well. I want to know how to prevent InfoPath from running server side code.... I want the same behaviour which we have in ASP.Net, if we apply validatoin to the controls.

     

    Any help in this regards would be heighly appreciated. Thanks in advance.

     

    Wednesday, December 28, 2011 6:44 AM

Answers

  • Hi Ali,

    On your button are you only checking the one field to see if it is blank?

    If so, you can just add that to your code.  At the top of the code get the value of the txtName field and then use an if statement for your code

    if (txtNameValue != "")

    XPathNavigator root = MainDataSource.CreateNavigator();
    
     XPathNavigator txtName = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:txtName", NamespaceManager);
                    string myTxtNameValue = txtName.Value;
    
    if (txtNameValue != "")
    {
       //your code here
    }
    

    Replace:  /my:myFields/my:txtName   with the xpath of your txtName field

     

    Wednesday, December 28, 2011 2:35 PM
  • Create a new boolean field as "IsError". Update this field in Action to True when condition is not met and to False when condition is met.

    As Melissa told, use this field as reference

    XPathNavigator root = MainDataSource.CreateNavigator();

    bool IsError = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:IsError", NamespaceManager).ValueAsBoolean;

    if (!IsError)

    {

      //Your Submit Code

    }

     

    By using this you only have to get vale of one field that is "IsError" if you are having many Client Side Rules.


    Dare to promise
    Wednesday, December 28, 2011 6:56 PM
  • In addition to the above, I would suggest also adding a conditional formatting rule to the button which disables it if the validation conditions are not met. This gives good visual feedback to the user.
    Tuesday, January 3, 2012 11:25 AM

All replies

  • Hi Ali,

    On your button are you only checking the one field to see if it is blank?

    If so, you can just add that to your code.  At the top of the code get the value of the txtName field and then use an if statement for your code

    if (txtNameValue != "")

    XPathNavigator root = MainDataSource.CreateNavigator();
    
     XPathNavigator txtName = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:txtName", NamespaceManager);
                    string myTxtNameValue = txtName.Value;
    
    if (txtNameValue != "")
    {
       //your code here
    }
    

    Replace:  /my:myFields/my:txtName   with the xpath of your txtName field

     

    Wednesday, December 28, 2011 2:35 PM
  • Create a new boolean field as "IsError". Update this field in Action to True when condition is not met and to False when condition is met.

    As Melissa told, use this field as reference

    XPathNavigator root = MainDataSource.CreateNavigator();

    bool IsError = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:IsError", NamespaceManager).ValueAsBoolean;

    if (!IsError)

    {

      //Your Submit Code

    }

     

    By using this you only have to get vale of one field that is "IsError" if you are having many Client Side Rules.


    Dare to promise
    Wednesday, December 28, 2011 6:56 PM
  • In addition to the above, I would suggest also adding a conditional formatting rule to the button which disables it if the validation conditions are not met. This gives good visual feedback to the user.
    Tuesday, January 3, 2012 11:25 AM