Is it possible to allow a user to skip a TInputDir

2019-02-17 16:09发布

问题:

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?

回答1:

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.



回答2:

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;