djacks
djacks

Reputation: 125

Does VSCODE.window.showInputBox provide a means to reject the input with a message and reprompt?

Is it possible using vscode.window.showInputBox to allow validation on the data entered and return an error message and reprompt?

Or do I have to use vscode.window.CreateInputBox and show/dispose onDidHide/ onDidAccept to provide this ability?

Upvotes: 1

Views: 3349

Answers (2)

skander
skander

Reputation: 1

You have to create a recursive call that would re-prompt for the input when it is not valid:

 function promptForInput(someArg) {
    vscode.window.showInputBox({
        title: `Some title`,
        prompt: `Some prompt message`
    }).then((value) => {
        if (!value) {   // check for validity
            vscode.window.showErrorMessage('Some message');
            promptForInput(someArg); // Recursive call
        } else {
            // Accept input
        }
    });
}

Upvotes: 0

Mark
Mark

Reputation: 182781

You can use this code:

  vscode.window.showInputBox({
    placeHolder: "howdy there",
    validateInput: text => {
      vscode.window.showInformationMessage(`Validating: ${text}`);  // you don't need this
      return text === '123' ? null : 'Not 123!';  // return null if validates
  }});

Return undefined, null, or the empty string when 'value' is valid.

inputBox validate demo

So it doesn't re-prompt with a new inputBox but shows a message in red when the input does not validate.

The user cannot Enter to accept invalid input, the user can only Escape to dispose of the InputBox.

You could vscode.window.showInformationMessage as part of the invalid process, something like:

vscode.window.showInputBox({
    placeHolder: "howdy there",
    validateInput: text => {
            // vscode.window.showInformationMessage(`Validating: ${text}`);
      if (text === '123') return null;
      else {
        if (text !== '') vscode.window.showInformationMessage(`Invalid: ${ text }.  Should be of form...`);
        return 'Not 123!';
      }
            // return text === '123' ? null : 'Not 123!';
  }});

But the input is evaluated on each character input which is cumbersome.

see, e.g., https://github.com/Microsoft/vscode-extension-samples/blob/master/quickinput-sample/src/basicInput.ts or https://github.com/Microsoft/vscode/issues/42763#issue-293656558

Upvotes: 5

Related Questions