Final Objective
The final objective is to have the ability to define custom validator for Google Forms item like requireTextMatchesPattern(pattern).
Use Case
This custom validator will be used for example to compare what the user enters in the form item field with more than one value. or at least to have a custom functionality to execute when the user enters not valid data in the field.
Example
I have 3 participants, I want to make a simple authentication mechanism to make sure that the targeted audiences are going to participate. I have a spreadsheet that contains 3 passwords. The first question in the Form will require the user to enter a password. If the password doesn't match with one of the stored passwords in the spreadsheet, then, a validation message will appear to the user.
Partially Solution
Based on this question we can make a simple validation using requireTextMatchesPattern validator or directly from UI. The problem is that this validator limits the compare values to one.
function validatePassword() {
// Create new custom form
var form = FormApp.create('New Form');
var ss = SpreadsheetApp.openById('SHEETID');
var password = ss.getSheetByName('SHEETNAME').getRange('A1').getValue();
// Create first question to check the password, it must be required so that the user does not have access to the rest
// of the form if failed to log in
var item = form.addTextItem().setRequired(true);
item.setTitle('TEST TITLE');
// Create validation for this question matching the password that we got from the sheet
var textValidation = FormApp.createTextValidation()
.setHelpText('You must enter the right password')
.requireTextMatchesPattern(password)
.build();
item.setValidation(textValidation);
}
What I am trying to do is to replace the .requireTextMatchesPattern(password) with a call to a custom validation function that does some validation process and then returns the type of TextValidationBuilder.
Research
I found this source code which defines an interface of TextValidationBuilder. I don't know if it is the key to accomplish the main objective.
Thanks!