Considering the following prompt:
var questions = [
{
type: "input",
name: "email",
message: "Enter recipients email"
}
];
Is it possible to repeat it a certain number of times? Basically I was wondering if the current state of Inquirer allowed me to achieve this functionality somehow.
Not entirely sure what you want to do.
questions = []
for (var i = 0; i < 10; i += 1) {
questions.push({
type: "input",
name: "email",
message: "Enter recipients email"
});
}
Inquirer let you validate answer, so you can check the validity of the email, and if it's not valid, then it'll ask the question again.
Sorry I didn´t explain correctly.
What I need is to prompt the user for something (emails in this case) but I
want to give him the choice to enter as many as he wants, so it´d be
something like:
[?] Enter an email // User enters an email
[?] Do you want to enter another email? (Y/n) // If yes repeat
And then to be able to access the list of emails somehow (an array maybe),
maybe there´s another way to achive this differently?
Right now I just display an "input" prompt and ask the user to enter the
list of emails separated by a "|" and later I parse them as an array so I
was just looking for a user-friendlier way to do the same.
On Sun, Sep 15, 2013 at 4:17 PM, Simon Boudrias [email protected]:
Inquirer let you validate answer, so you can check the validity of the
email, and if it's not valid, then it'll ask the question again.—
Reply to this email directly or view it on GitHubhttps://github.com/SBoudrias/Inquirer.js/issues/67#issuecomment-24479835
.
Ing. Javier Villanueva
Hello, I think the way you are doing it now is more user friendly. Maybe a comma separator would be better than "|".
Below is a sample of what you want to accomplish:
var
inquirer = require('inquirer'),
someLib = { isEmail: function () { return true; } };
var getEmails = function (emails, callback) {
inquirer.prompt({
type: 'input',
name: 'email',
message: 'Enter an email:',
validate: function (input) {
if (someLib.isEmail(input)) {
return true;
}
return 'Email address is not valid. Please try again...';
}
}, function (reply) {
emails.push(reply.email);
inquirer.prompt({
type: 'confirm',
name: 'confirmed',
message: 'Do you want to enter another email?',
default: false
}, function (reply) {
if (reply.confirmed) {
getEmails(emails, function (mails) {
callback(mails);
});
} else {
callback(emails);
}
});
});
};
getEmails([], function (emails) {
console.log('emails:', emails);
});
Thanks a lot, I know right now my solution looks friendlier but the app
might need more fields later on besides the email so I'll need to add more
separators to make it work. It's nice to have options.
On Sun, Sep 15, 2013 at 10:42 PM, mpal9000 [email protected] wrote:
Hello, I think the way you are doing it now is more user friendly. Maybe a
comma separator would be better than "|".Below is a sample of what you want to accomplish:
var
inquirer = require('inquirer'),
someLib = { isEmail: function () { return true; } };
var getEmails = function (emails, callback) {
inquirer.prompt({type: 'input', name: 'email', message: 'Enter an email:', validate: function (input) { if (someLib.isEmail(input)) { return true; } return 'Email address is not valid. Please try again...' }}, function (reply) {
emails.push(reply.email);inquirer.prompt({ type: 'confirm', name: 'confirmed', message: 'Do you want to enter another email?', default: false }, function (reply) { if (reply.confirmed) { getEmails(emails, function () { callback(emails); }); } else { callback(emails); } });});};
getEmails([], function (emails) {
console.log('emails:', emails);});—
Reply to this email directly or view it on GitHubhttps://github.com/SBoudrias/Inquirer.js/issues/67#issuecomment-24487067
.
Ing. Javier Villanueva
BTW, you don't need to nest the second inquirer.prompt call, but the solution is the way to go. Recurse over the same screen (in the example called getEmails) until the user opt-out.
Most helpful comment
Sorry I didn´t explain correctly.
What I need is to prompt the user for something (emails in this case) but I
want to give him the choice to enter as many as he wants, so it´d be
something like:
And then to be able to access the list of emails somehow (an array maybe),
maybe there´s another way to achive this differently?
Right now I just display an "input" prompt and ask the user to enter the
list of emails separated by a "|" and later I parse them as an array so I
was just looking for a user-friendlier way to do the same.
On Sun, Sep 15, 2013 at 4:17 PM, Simon Boudrias [email protected]:
Ing. Javier Villanueva