# Form Server Validations: Generic Form Validator using Javascript

**URL:** <https://forum.camunda.io/t/form-server-validations-generic-form-validator-using-javascript/6897>\
**Category:** Discussion & Questions\
**Tags:** process-engine\
**Created:** [April 12, 2018, 7:43pm UTC](https://forum.camunda.io/t/form-server-validations-generic-form-validator-using-javascript/6897 "2018-04-12T19:43:04Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![StephenOTT](https://forum.camunda.io/letter_avatar_proxy/v4/letter/s/d2c977/32.png) [@StephenOTT](https://forum.camunda.io/u/StephenOTT)\
**Post date:** [April 12, 2018, 7:43pm UTC](https://forum.camunda.io/t/form-server-validations-generic-form-validator-using-javascript/6897/1 "2018-04-12T19:43:04Z")

</div>

Hey!

Little magic 🎉 today!

Generally we buld HTML forms that are “Embedded forms” with the Angular API. These are client side/tasklist forms, but do not provide any form of validation on the server side. This is a problem for two reasons:

1. Does not provide good submission security checks
2. System to System interactions can bypass the validations that are used in the Angular forms.

So how do we fix this problem?

Well in the past we have to write a java class that does a custom validation. This was generally done on a per field basis or you add extra logic.

This is a huge pain because the BPMN and the Form are built rapidly, but when we need to generate the sever validations, we are stuck with compile jars and redeployment… No good!

So I have implemented the Camunda Form Validator: JS Server.

> **[GitHub - StephenOTT/camunda-form-validator-js-server: Camunda BPM Form...](https://github.com/StephenOTT/camunda-form-validator-js-server)**
>
> Camunda BPM Form Validator plugin, enabling support for Javascript validation of Form Submissions for Start Events and User Tasks - GitHub - StephenOTT/camunda-form-validator-js-server: Camunda BPM...

This is a Generic Form Validation solution that accesses Camunda’s Nashorn Script Engine to execute the validation.

It lets you add a form-validation.js script to your deployment, and with a simple configuration in the modeler on the Start Event or User Task form(s) you get your submissions added to the JS Validator.

 ![Camunda-Modeler-with-formkey](https://forum.camunda.io/uploads/default/original/2X/c/c652d500479df34b6708a77762569d0f1f7d4441.png)

 ![Camunda-Modeler-without-formkey](https://forum.camunda.io/uploads/default/original/2X/a/a27f77d63cf3202a0c0ecd709316ce42cc25ce3e.png)

 ![Tasklist-validation-error-message](https://forum.camunda.io/uploads/default/original/2X/f/f78cee8953273670c4800e277bcc68f065fbe63e.png)

your form-validation.js could be as simple as:

```javascript
load('classpath:validationResult.js')

// The submissionsValues variable comes from the engine binding
validateData(submissionValues)

function validateData(values){
  if (values.containsKey('firstName') == false){
    return validationResult(false, {
                                     "detail": "FIELD_REQUIRED", 
                                     "message": "firstName is required"
                                    })
  }

  if (values.containsKey('age') && values['age'] < 18 ){
    return validationResult(false, {
                                     "detail": "AGE_LIMIT", 
                                     "message": "age must be 18 or older"
                                    })
  }
  
  // If no errors were found:
  return validationResult(true)
}

```

Check out the Github repo for the Jar and further usage examples. I have also included a ready to build docker setup that will let you test it out. See the Readme of the Repo.

Would love feedback and usage examples that come to mind.

Also [@camunda](https://forum.camunda.io/groups/camunda) it would be great is the following issue could be resolved, as it is a QOL blocker from the actual thrown errors being returned in the REST API and thus Tasklist:  
[https://app.camunda.com/jira/browse/CAM-8276](https://app.camunda.com/jira/browse/CAM-8276)

---

<div class="post-metadata">

**Author:** ![StephenOTT](https://forum.camunda.io/letter_avatar_proxy/v4/letter/s/d2c977/32.png) [@StephenOTT](https://forum.camunda.io/u/StephenOTT)\
**Post date:** [April 13, 2018, 4:49pm UTC](https://forum.camunda.io/t/form-server-validations-generic-form-validator-using-javascript/6897/2 "2018-04-13T16:49:50Z")

</div>

I have updated this usage with Validate.js usage:

```javascript
// Validate.js Constraints
function getConstraints() {
  var constraints = {
    age: {
      presence: true,
      numericality: {
        onlyInteger: true,
        greaterThan: 18,
        lessThanOrEqualTo: 125,
      }
    }
  };
  return constraints
}

var validation = validate(jsonSubmission, getConstraints())

if (validation === undefined) {
  validationResult(true)
} else {
  validationResult(false, {
                            "detail": "VALIDATE.JS", 
                            "message": JSON.stringify(validation)
                          }
                    )
}

```

see the docs for further info

---

<div class="post-metadata">

**Author:** ![StephenOTT](https://forum.camunda.io/letter_avatar_proxy/v4/letter/s/d2c977/32.png) [@StephenOTT](https://forum.camunda.io/u/StephenOTT)\
**Post date:** [April 17, 2018, 4:46pm UTC](https://forum.camunda.io/t/form-server-validations-generic-form-validator-using-javascript/6897/3 "2018-04-17T16:46:30Z")

</div>

I have added Dynamic script calls through property configuration on the validator field:

 ![validator](https://forum.camunda.io/uploads/default/original/2X/8/8feffbd99c4245ff01f410825134ab7ef26ca603.png)

see the v0.3.0 release at:

> **[Release v0.3.0 - Dynamic JS file selection ·...](https://github.com/StephenOTT/camunda-form-validator-js-server/releases/tag/v0.3.0)**
>
> Added ability to define specific javascript files on a validator. This enables the different validator files/js to be used on Start Events and User Tasks, rather than a single js file which tends ...

and see docs: that example usage: [GitHub - StephenOTT/camunda-form-validator-js-server: Camunda BPM Form Validator plugin, enabling support for Javascript validation of Form Submissions for Start Events and User Tasks](https://github.com/StephenOTT/camunda-form-validator-js-server#usage)

TLDR:

1. Add a `validator_file` property to the field that contains the .js filename (`myscript.js`).
2. If you do not provide a `validator_file` property then a default will be used as fieldIdValue.js (example: given the field id is `server_validation` then it will default to looking for `server_validation.js` in the deployment.
3. You can layer validations by adding multiple fields that call the JsFormValidation validator and call different validation files. layering lets you abstract common/reusable validators. (Also note that you can do similar by using the `load()` function in nashorn and just call a single script. Layering is provide for further configuration options.

---

<div class="post-metadata">

**Author:** ![StephenOTT](https://forum.camunda.io/letter_avatar_proxy/v4/letter/s/d2c977/32.png) [@StephenOTT](https://forum.camunda.io/u/StephenOTT)\
**Post date:** [April 18, 2018, 4:31pm UTC](https://forum.camunda.io/t/form-server-validations-generic-form-validator-using-javascript/6897/4 "2018-04-18T16:31:01Z")

</div>

Added performance enhancements and debug logging for a 0.4.0 release.

> **[Release Performance enhancement and Debug Logging ·...](https://github.com/StephenOTT/camunda-form-validator-js-server/releases/tag/v0.4.0)**
>
> Increased overall performance. Moved ScriptEngine generation into static variable generation of JsFormValidation class to ensure cached engine is used. This has decreased form submission time dow...

ScriptEngine variables are now static at the class level along with the Camunda Script Resolver cache usage to ensure that scripts are cached into a common engine rather than creating new engines for every submission. This leads to sub 75-80ms response times even when using validate.js in a `load()` function. Previous response was 500-800ms.

Note that first submission will always be longer as the cache needs to be warmed up.

---

<div class="post-metadata">

**Author:** ![StephenOTT](https://forum.camunda.io/letter_avatar_proxy/v4/letter/s/d2c977/32.png) [@StephenOTT](https://forum.camunda.io/u/StephenOTT)\
**Post date:** [April 18, 2018, 6:42pm UTC](https://forum.camunda.io/t/form-server-validations-generic-form-validator-using-javascript/6897/5 "2018-04-18T18:42:55Z")

</div>

I have updated with a “banned field” example where a user task has a validation restriction of a list of banned fields that are not allowed to be modified.

> **[camunda-form-validator-js-server/bpmn/banned-fields-example at master ·...](https://github.com/StephenOTT/camunda-form-validator-js-server/tree/master/bpmn/banned-fields-example)**
>
> master/bpmn/banned-fields-example

```nohighlight
load('classpath:validationResult.js')
load('classpath:validate.min.js')
var JSONObject = Java.type('org.camunda.bpm.engine.impl.util.json.JSONObject')

var jsonSubmission = JSON.parse(new JSONObject(submissionValues).toString())

// Validate.js Constraints
function getConstraints() {
  var constraints = {
    age: {
      presence: true,
      numericality: {
        onlyInteger: true,
        greaterThan: 18,
        lessThanOrEqualTo: 125,
      }
    }
  };
  return constraints
}

// List of fields that are not allowed to be changed
function bannedFields(){
  // could also be loaded from another location (like a yaml or json file)
  return [
    "risk",
    "owner",
    "master_field",
    "current_state",
    "flagged",
    "priority"
  ]
}

// Check if the submission has any of the banned fields
function checkForBannedFields(submission, bannedFields) {
  // Loop through list of banned fields (nashorn loop)
  for each (var field in bannedFields){
    var hasBannedField = validate.contains(submission, field)
    // if a banned field was found then return true:
    if (hasBannedField == true) {
      return true
    }
  }
  // If no banned fields were found:
  return false
}

// if there is a banned field:
if (checkForBannedFields(jsonSubmission, bannedFields())){
  validationResult(false, {
                          "detail": "VALIDATE.JS", 
                          "message": 'Submission contains a banned field: ' + JSON.stringify(bannedFields())
                        }
                  )

// If no banned fields were found:
} else {
  // If no banned fields then continue:
  // Run Validations against Validate.js
  var validation = validate(jsonSubmission, getConstraints())

  if (validation === undefined) {
    validationResult(true)
  } else {
    validationResult(false, {
                              "detail": "VALIDATE.JS", 
                              "message": JSON.stringify(validation)
                            }
                      )
  }
}

```

---

<div class="post-metadata">

**Author:** ![StephenOTT](https://forum.camunda.io/letter_avatar_proxy/v4/letter/s/d2c977/32.png) [@StephenOTT](https://forum.camunda.io/u/StephenOTT)\
**Post date:** [May 8, 2018, 4:49pm UTC](https://forum.camunda.io/t/form-server-validations-generic-form-validator-using-javascript/6897/6 "2018-05-08T16:49:52Z")

</div>

PR for patch [Add FormFieldValidationException Catching in the API by StephenOTT · Pull Request #296 · camunda/camunda-bpm-platform · GitHub](https://github.com/camunda/camunda-bpm-platform/pull/296)

---

<div class="post-metadata">

**Author:** ![Laxman\_Kumar](https://forum.camunda.io/letter_avatar_proxy/v4/letter/l/9fc29f/32.png) [@Laxman\_Kumar](https://forum.camunda.io/u/Laxman_Kumar)\
**Post date:** [March 18, 2019, 9:44am UTC](https://forum.camunda.io/t/form-server-validations-generic-form-validator-using-javascript/6897/7 "2019-03-18T09:44:49Z")

</div>

Hi @StephenOTT  
I am getting below error when I hit below API

localhost:8080/engine-rest/process-definition/key/js-form-validation-test-1/submit-form

{  
“type”: “RestException”,  
“message”: “Cannot instantiate process definition js-form-validation-test-1:1:9f09d020-495c-11e9-898f-02423270994f: ENGINE-09008 Exception while instantiating class ‘io.digitalstate.camunda.JsFormValidation’: ENGINE-09017 Cannot load class ‘io.digitalstate.camunda.JsFormValidation’: io.digitalstate.camunda.JsFormValidation”  
}

---

<div class="post-metadata">

**Author:** ![StephenOTT](https://forum.camunda.io/letter_avatar_proxy/v4/letter/s/d2c977/32.png) [@StephenOTT](https://forum.camunda.io/u/StephenOTT)\
**Post date:** [March 18, 2019, 10:43am UTC](https://forum.camunda.io/t/form-server-validations-generic-form-validator-using-javascript/6897/8 "2019-03-18T10:43:15Z")

</div>

You have to provide more details. How did you install the plugin? What env, what Camunda version, which Bpmn etc.

---

<div class="post-metadata">

**Author:** ![Laxman\_Kumar](https://forum.camunda.io/letter_avatar_proxy/v4/letter/l/9fc29f/32.png) [@Laxman\_Kumar](https://forum.camunda.io/u/Laxman_Kumar)\
**Post date:** [March 18, 2019, 11:24am UTC](https://forum.camunda.io/t/form-server-validations-generic-form-validator-using-javascript/6897/9 "2019-03-18T11:24:03Z")

</div>

HI @StephenOTT ,  
Thank you for replying  
I am following your git tutorial

> **[GitHub - StephenOTT/camunda-form-validator-js-server: Camunda BPM Form...](https://github.com/StephenOTT/camunda-form-validator-js-server)**
>
> Camunda BPM Form Validator plugin, enabling support for Javascript validation of Form Submissions for Start Events and User Tasks - GitHub - StephenOTT/camunda-form-validator-js-server: Camunda BPM...

I am using the latest version of camunda

you mentioned in git **Add the jar to the Camunda classpath.** But I don’t’ know where I need to add.  
can you tell me where I need to keep this JAR FIle

 ![Screenshot%20from%202019-03-18%2016-52-36](https://forum.camunda.io/uploads/default/original/2X/5/52609b666c5f38c2cd7c68923bbc6d2bef56cdbd.png)

---

<div class="post-metadata">

**Author:** ![Laxman\_Kumar](https://forum.camunda.io/letter_avatar_proxy/v4/letter/l/9fc29f/32.png) [@Laxman\_Kumar](https://forum.camunda.io/u/Laxman_Kumar)\
**Post date:** [March 18, 2019, 4:08pm UTC](https://forum.camunda.io/t/form-server-validations-generic-form-validator-using-javascript/6897/10 "2019-03-18T16:08:39Z")

</div>

Hi @StephenOTT  
can you tell me, if I want to use pure javascript in my form validation. I don’t want to use any JAR file or java file

---

<div class="post-metadata">

**Author:** ![StephenOTT](https://forum.camunda.io/letter_avatar_proxy/v4/letter/s/d2c977/32.png) [@StephenOTT](https://forum.camunda.io/u/StephenOTT)\
**Post date:** [March 18, 2019, 8:22pm UTC](https://forum.camunda.io/t/form-server-validations-generic-form-validator-using-javascript/6897/11 "2019-03-18T20:22:43Z")

</div>

If you are not using the jar plugin then you have to validate with a execution listener but will be subject to the limitations that the plugin was built to overcome.

---

<div class="post-metadata">

**Author:** ![Laxman\_Kumar](https://forum.camunda.io/letter_avatar_proxy/v4/letter/l/9fc29f/32.png) [@Laxman\_Kumar](https://forum.camunda.io/u/Laxman_Kumar)\
**Post date:** [March 19, 2019, 4:07am UTC](https://forum.camunda.io/t/form-server-validations-generic-form-validator-using-javascript/6897/12 "2019-03-19T04:07:37Z")

</div>

Thanks  
can you tell me where I need to add JAR file

---

<div class="post-metadata">

**Author:** ![StephenOTT](https://forum.camunda.io/letter_avatar_proxy/v4/letter/s/d2c977/32.png) [@StephenOTT](https://forum.camunda.io/u/StephenOTT)\
**Post date:** [March 19, 2019, 12:26pm UTC](https://forum.camunda.io/t/form-server-validations-generic-form-validator-using-javascript/6897/13 "2019-03-19T12:26:40Z")

</div>

The jar goes into the /camunda/lib folder of your Camunda installation

---

<div class="post-metadata">

**Author:** ![Laxman\_Kumar](https://forum.camunda.io/letter_avatar_proxy/v4/letter/l/9fc29f/32.png) [@Laxman\_Kumar](https://forum.camunda.io/u/Laxman_Kumar)\
**Post date:** [March 19, 2019, 12:29pm UTC](https://forum.camunda.io/t/form-server-validations-generic-form-validator-using-javascript/6897/14 "2019-03-19T12:29:01Z")

</div>

Thanks @StephenOTT  
Now it’s working fine

---

<div class="post-metadata">

**Author:** ![StephenOTT](https://forum.camunda.io/letter_avatar_proxy/v4/letter/s/d2c977/32.png) [@StephenOTT](https://forum.camunda.io/u/StephenOTT)\
**Post date:** [March 19, 2019, 2:36pm UTC](https://forum.camunda.io/t/form-server-validations-generic-form-validator-using-javascript/6897/15 "2019-03-19T14:36:11Z")

</div>

Please star the GitHub project repo so we can know what is popular and being used. 👍🏻

---

<div class="post-metadata">

**Author:** ![khangm](https://forum.camunda.io/user_avatar/forum.camunda.io/khangm/32/10625_2.png) [@khangm](https://forum.camunda.io/u/khangm)\
**Post date:** [July 13, 2020, 11:10am UTC](https://forum.camunda.io/t/form-server-validations-generic-form-validator-using-javascript/6897/16 "2020-07-13T11:10:06Z")

</div>

Hello Stephen,

Thanks for this tutorial. I have added the **digitalstate.camunda.js.server.form.validation-0.4.0-SNAPSHOT.jar** in by **/camunnda/lib** folder. But when I am trying to hit the API (following your tutorial in github) I am getting exception

`Cannot instantiate process definition c89cf5e4-c4f6-11ea-b2bc-0242cf66ffca: ENGINE-09008 Exception while instantiating class 'io.digitalstate.camunda.JsFormValidation': ENGINE-09017 Cannot load class 'io.digitalstate.camunda.JsFormValidation': io.digitalstate.camunda.JsFormValidation`

I think it is unable to find **JsFormValidation** in the classpath of Camunda Server. Can you please help what I am missing? Thanks

---

<div class="post-metadata">

**Author:** ![system](https://forum.camunda.io/uploads/default/original/3X/5/8/58d50142eacca605d35cb1ef9e6b86ae85f49436.png) [@system](https://forum.camunda.io/u/system)\
**Post date:** [January 30, 2024, 11:11am UTC](https://forum.camunda.io/t/form-server-validations-generic-form-validator-using-javascript/6897/17 "2024-01-30T11:11:04Z")

</div>

Closed on January 30, 2024, 11:11am UTC


