Search code examples
wixwix3.7

To validate the input controls in WIX Installer


I'm new to WIX. I wanted to validate the input controls like Textbox is not null and password and confirm password is same. I tried to do it in the custom action,but i couldn't send the parameters. If at all i send the parameter how to the return the values with the to stay in the same installation page.

<Dialog Id="XXX" Width="370" Height="270" Title="Installation">
<Control Id="Next" Type="PushButton" X="236" Y="243" Width="56" Height="17" Default="yes" Text="!(loc.WixUINext)" >
<Publish Event="DoAction" Value="CheckingPID">1</Publish>
<Publish Event="SpawnDialog" Value="InvalidPidDlg">PIDACCEPTED = "0"</Publish>
<Control Id="Usernamelbl" Type="Text" X="20" Y="100" Width="95" Height="10" NoPrefix="yes" Property="WIXUI_INSTALLDIR" Text="Username:" />
<Control Id="UsernameVal" Type="Edit" X="125" Y="100" Width="200" Height="17" Property="SETUSERNAME"  Indirect="no" Disabled="no" />

</Dialog>
<CustomAction Id="CheckingPID"  BinaryKey="CustomActionBinary"   Impersonate="no"  DllEntry="Validate"  Execute="immediate" Return="check"/>
  [CustomAction]
        public static ActionResult Validate(Session session)
        {
        MessageBox.Show(Session.CustomActionData["SETUSERNAME"]);
        
            return ActionResult.Success;
        }

Is this the right way to validate or any other way to validate.

Thanks in advance


Solution

  • Here is a Working Custom Wix Dialog

    This code creates a custom UI Dialog with Custom Actions. Purpose of this Dialog is while we are installing a Desktop Application we can setup Db Connection information.

    DbConnectionInfo.wxs

    <?xml version="1.0" encoding="UTF-8"?>
    <Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
      <Fragment>
        <Property Id="Server" Value="127.0.0.1" />
        <Property Id="Port" Value="XXX" />
        <Property Id="Database" Value="DbName" />
        <Property Id="User" Value="root" />
        <Property Id="Password" Value="1234abcA" />
        <UI Id="DbConnectionDlgUI">
    
          <Dialog Id="DbConnectionDlg" Width="400" Height="275" Title="Demo : Database Connection Settings">
    
            <Control Id="headerText"  Type="Text" X="140" Y="10" Width="260" Height="40" Transparent="no"
                     Text="{\WixUI_Font_Title}Database Connection Settings Screen" />
    
            <Control Id="SideBar" Type="Bitmap" Text="WixUIBannerBmp" X="0" Y="0" Height="240" Width="130" Image="yes" />
    
            <Control Id="explanationText" X="140" Y="50" NoWrap="no" RightAligned="no" Transparent="yes"
                     Type="Text" Width="260" Height="100"
                     Text="{\WixUI_Font_Normal}Before you can use this Service, you need to provide your My Sql Connection settings which is used getting the email database information. If you choose not to install this application, click on the Cancel button to exit." />
    
            <Control Id="ServerLabel" Type="Text" X="160" Y="120" Height="17" Width="65" Transparent="yes" Text="{\WixUI_Font_Normal}Server:" />
            <Control Id="ServerTextBox" Type="Edit" X="230" Y="120"   Height="17" Width="60" Property="Server" />
    
            <Control Id="PortLabel" Type="Text" X="295" Y="120" Height="17" Width="21" Transparent="yes" Text="{\WixUI_Font_Normal}Port:" />
            <Control Id="PortTextBox" Type="Edit" X="325" Y="120"   Height="17" Width="30" Property="Port" />
    
            <Control Id="DatabaseLabel" Type="Text" X="160" Y="140" Height="17" Width="65" Transparent="yes" Text="{\WixUI_Font_Normal}Database:" />
            <Control Id="DatabaseTextbox" Type="Edit" X="230" Y="140"  Height="17" Width="120" Property="Database" />
    
            <Control Id="UserLabel" Type="Text" X="160" Y="160" Height="17" Width="65" Transparent="yes" Text="{\WixUI_Font_Normal}User:" />
            <Control Id="UserTextbox" Type="Edit" X="230" Y="160"  Height="17" Width="120" Property="User" />
    
            <Control Id="PasswordLabel" Type="Text" X="160" Y="180" Height="17" Width="65" Transparent="yes" Text="{\WixUI_Font_Normal}Password:" />
            <Control Id="PasswordTextbox" Type="Edit" X="230" Y="180"  Height="17" Width="120" Property="Password" Password="yes"/>
    
            <Control Id="bottomLine" Type="Line" X="130" Y="239" Width="270" Height="1"/>
    
            <Control Id="Back" Type="PushButton" Text="Back"  X="208" Y="248" Height="17" Width="60" >
            </Control>
            <Control Id="Next" Type="PushButton" Text="Next"  X="269" Y="248" Height="17" Width="60" Default="yes">
              <Publish Event="DoAction" Value="CreateDbConnectionProperties">1</Publish>
            </Control>
            <Control Id="Cancel" Type="PushButton" Text="Cancel" X="330" Y="248" Height="17" Width="60" Cancel="yes">
              <Publish Event="DoAction" Value="CleanUpAction">1</Publish>
              <Publish Event="NewDialog" Value="CancelDlg" Order="2">1</Publish>
            </Control>
    
          </Dialog>
        </UI>
      </Fragment>
    
      <Fragment>    
        <CustomAction Id="CreateDbConnectionProperties" BinaryKey="CustomActionBinary" DllEntry="CreateDbConnectionProperties"  />
      </Fragment>
    </Wix>
    

    CustomAction.cs

    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.Deployment.WindowsInstaller;
    using System.IO;
    
    namespace Demo.InstallerActions
    {
        public class CustomlActions
        {
            private readonly static string AppName = "Demo";
           
            [CustomAction]
            public static ActionResult CreateDbConnectionProperties(Session session)
            {
                session.Log("Saving Db Details Started.");
                try
                {
                    string Server = session["Server"].Encrypt(AppConstants.SecurityKey,true);
                    string Database = session["Database"].Encrypt(AppConstants.SecurityKey, true);
                    string User = session["User"].Encrypt(AppConstants.SecurityKey, true);
                    string Password = session["Password"].Encrypt(AppConstants.SecurityKey, true);
                    string Port = session["Port"].Encrypt(AppConstants.SecurityKey, true);
    
                    string[] confData = { Server, Database, User, Password, Port };
    
                    string appdataPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
    
                    if (!Directory.Exists($"{appdataPath}\\{AppName}"))
                    {
                        Directory.CreateDirectory($"{appdataPath}\\{AppName}");
                    }
    
                    File.WriteAllLines($"{appdataPath}\\{AppName}\\conf.sys", confData);
    
                    session.Log("Db Details Saved");
    
                    return ActionResult.Success;
                }
                catch (Exception ex)
                {
                    session.Log($"Configuration File Creation Failed with Error: {ex.Message}");
                    return ActionResult.Failure;
                }
            }
    
            [CustomAction]
            public static ActionResult CleanUpAction(Session session)
            {
                session.Log("Cleanup Started.");
                try
                {
                    string appdataPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
    
                    if (!Directory.Exists($"{appdataPath}\\{AppName}"))
                        Directory.Delete($"{appdataPath}\\{AppName}", true);
    
                    session.Log("Db Details Saved");
    
                    return ActionResult.Success;
                }
                catch (Exception ex)
                {
                    session.Log($"Cleanup Error: {ex.Message}");
                    return ActionResult.Failure;
                }
            }
        }
    }