0

I was wondering if it is possible to validate a container bound htmlService HTML Form server-side with GAS, to prevent a User from entering Invalid Data into a sheet, for example.

If it is possible to handle the form validation server-side, how would one go about doing so?

Rubén
  • 34,714
  • 9
  • 70
  • 166
Rookie_Js
  • 31
  • 5
  • I have to apologize for my poor English skill. Unfortunately, I cannot understand your question from `I was wondering if it is possible to validate a container bound htmlService HTML Form server-side with GAS, to prevent a User from entering Invalid Data into a sheet, for example. If it is possible to handle the form validation server-side, how would one go about doing so?`. Can I ask you about the detail of your current issue and your goal? – Tanaike Sep 17 '22 at 23:51
  • 2
    Yes. It is possible – Cooper Sep 18 '22 at 00:12

1 Answers1

2

Google Apps Script hasn't a built-in way to do server-side form validation. You have to design the way that the form validation will be handled and how the feedback will be given to the web-app user.

A very simple validation might be to validate that the form submission has all the fields.

/**
 * Put this in a .gs file. It might be called from the client-side
code
 * by using google.script.run, using withSuccessHandler  to handle 
the boolean response.
 */
function validate(formData){
  const expectedFields = ['field1','field2'];
  const formFields = Object.keys(formData);
  const isValid = expectedFields.every(field => formFields.includes(field))
  return isValid;
}
/**
 * Call this from a form submit event on the client-side.
 * Usually this kind of code is included in a .html file between <script> tags.
 */
function handleFormSubmit(){
  google.script.run
  .withSuccessHandler(validity => {
    if(validity){
      // put here the code to confirm the that the form submission is valid
    } else {
      // put here the code to confirm the that the form submission is not valid 
    }
  })
  .withFailureHandler(error => {
    // put here the code to handle server-side errors
  }).validate(event.currentTarget)
}

There are some frameworks and communities around them that might help you on this task, i.e, Bootstrap, jQuery. Also there several are forms for sharing code like Node.js among many other that could have libraries / modules / packages that might be used with Google Apps Script.

Please bear in mind that doing server-side form validation in Google Apps Script might make your web app respond slowly as google.script.run (required for server-client communication) might be slow due to multiple factors, being one of them calling Google Apps Script services like SpreadsheetApp.

One trick that might help you handle the data-validation work is to write the validation rules some way that they could be used on the client and server sides. One way to do this is by using a .gs file to hold the JavaScript and use ScriptApp.getResource(filename).getDataAsString(), an undocumented method that have being working for years, to get this code and to insert it into a HtmlOutput object either by using a templated HTML or by using HtmlOutput.append(addedContent).

Related

Rubén
  • 34,714
  • 9
  • 70
  • 166