7

I have an installer using Inno Setup that allows the user to select a file location, at install time. The file is kind of like an answers file to help with installation.

For this prompt, I'm using the TInputDirWizardPage.

It works fine when the user is using this file, but if he doesn't wish to, it automatically throws an error telling him that he must enter a path.

Is there a way to NOT force validation so that the user can just hit next and let me figure out that he doesn't have a file?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Mike
  • 1,246
  • 3
  • 20
  • 34

2 Answers2

5

As TLama said, there's no way to NOT force validation, it's hardcoded. But that doesn't mean we can't hack the hell out of it!!!

In the example below we override the form NextButton click and if the directory is empty we simply put a value to pass the validation, after that we can clear the directory.

var
  InputDirPage: TInputDirWizardPage;
  Old_WizardForm_NextButton_OnClick: TNotifyEvent;

procedure WizardForm_NextButton_OnClick(Sender: TObject);
var
    IsDirEmpty: Boolean;
begin
    if (WizardForm.CurPageID = InputDirPage.ID) and (InputDirPage.Values[0] = '') then
    begin
        IsDirEmpty := True;
        InputDirPage.Values[0] := WinDir; { Force value to pass validation }
    end;

    Old_WizardForm_NextButton_OnClick(Sender);

    if IsDirEmpty then
        InputDirPage.Values[0] := '';
end;

procedure InitializeWizard();
begin
    InputDirPage := CreateInputDirPage(
        wpWelcome,      { AfterID }
        'ACaption',
        'ADescription',
        'ASubCaption',
        False,          { AAppendDir }
        ''              { ANewFolderName }
    );  

    InputDirPage.Add('doc dir:');

    { override wizard NextButton click }
    Old_WizardForm_NextButton_OnClick := WizardForm.NextButton.OnClick;
    WizardForm.NextButton.OnClick := @WizardForm_NextButton_OnClick;
end;
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
PLopes
  • 93
  • 2
  • 6
  • Defintely! I missed that you can store the `OnClick` event method and call it by yourself at that time. This code you may improve by some *default directory* or some flag that will pass the information that the user had left the field empty so you can decide what to do with the input. – TLama Jul 29 '15 at 17:07
4

Unfortunately no. This is hardcoded in the TInputDirWizardPage.NextButtonClick method, which internally validates all the edit boxes by calling ValidateCustomDirEdit function, which doesn't care if the edit has been left intentionally empty; it just checks if it contains a valid directory path. Or, in other words, the TInputDirWizardPage fields are not optional, they must contain valid paths at this time.

Well, I don't feel this was an expected behavior. If you compare file and dir input page, they differ. Whilst in the file input page you can leave edit boxes empty, in dir input page you cannot. I think, that would be enough if there would be a check if the edit box is not empty and only if it's not, validate its content. You would be able to check whether the edit is empty by yourself (if you'd require mandatory field), and stop the user on that page, but you are not able to suppress that validation if the edit is empty.

In your situation I would consider using TInputFileWizardPage as you were talking about a file input, or creating your own directory input page.

TLama
  • 75,147
  • 17
  • 214
  • 392
  • That's what I feared. Last night I also tried putting my own "skip" button on the page, which works, but can't seem to get the installer to actually move onto the next page when it's clicked. Thanks for the info. – Mike Jul 30 '14 at 13:02
  • 2
    That `NextButtonClick` method is called internally and there's no way to intercept it, so you're out of luck with `TInputDirWizardPage` with this situation. But making your own dir input page is not that hard. And it may even accept the selected folder only if the file you're looking for is there. – TLama Jul 30 '14 at 13:07