Validate form from javascript

Questions and discussion about using ProcessMaker: user interface, running cases & functionality

Moderator: amosbatto

allanrp1
Posts: 8
Joined: Wed Apr 11, 2018 2:16 pm
Country: United States of America

Validate form from javascript

Unread postby allanrp1 » Mon Apr 16, 2018 11:29 am

Hi.
I'm looking for a way to validate a form in javascript, and check if it's filled.
I saw there was a method in 2.8 "validateForm", but i don't see it in 3.2

How can i get that?

amosbatto
Posts: 6143
Joined: Mon Jun 22, 2009 10:28 am
Country: Bolivia
Location: La Paz
Contact:

Re: Validate form from javascript

Unread postby amosbatto » Mon Apr 16, 2018 11:31 am

There is a "required" property for most types of fields and there is a "validate" property in text fields where you can enter regular expressions to validate the input. See:
http://wiki.processmaker.com/3.2/DynaForm_Field_Properties#Required
http://wiki.processmaker.com/3.0/Text_and_Textarea_Controls#Validate

I don't know of anyway to validate all the fields at once with JavaScript, except call grid.saveForm() or grid.submitForm().
Amos B. Batto ♦ ProcessMaker Technical Documentation Writer & Forum Manager ♦ Cofounder of ILLA (http://www.illa-a.org)

allanrp1
Posts: 8
Joined: Wed Apr 11, 2018 2:16 pm
Country: United States of America

Re: Validate form from javascript

Unread postby allanrp1 » Mon Apr 16, 2018 12:08 pm

So it's not possible...
What can i do? i want to execute a script after a web entry, is there some way to automatically pass to another task without a user review?

amosbatto
Posts: 6143
Joined: Mon Jun 22, 2009 10:28 am
Country: Bolivia
Location: La Paz
Contact:

Re: Validate form from javascript

Unread postby amosbatto » Mon Apr 16, 2018 12:30 pm

I looked through the source code in workflow/public_html/lib/pmdynaform/build/js/PMDynaform.js:5962 and found the form.isValid() function.
It will validate all the field in the Dynaform. You can call it like this:
getFormById("form-id").isValid()

It returns true if all fields are good and false if any are required fields which are blank or any fields that don't match their validate regular expressions. It also shows red error messages under the fields that aren't validated in the Dynaform.

For example:

Code: Select all

var formId = $("form").prop("id");
if (getFormById(formId)isValid == true) {
   $("#"+formId).saveForm();
}


If you want to validate a particular field you can call:
getFieldById("field-id").validate()

It shows the red error message for an empty required field or a text field which doesn't match its validate regular expression. Otherwise is shows nothing in the Dynaform. The function returns the field's model object, which can be examined.

If you want to remove or add the red asterisk (*) next to fields to show that they are required, then use the following methods:
getFieldById("field-id").showRequired()
getFieldById("field-id").hideRequired()

These methods can only be used on fields that are marked as "required" or have a defined "validate" property. Unless you have a good reason to hide the asterisk, it is recommended to use the enableValidation() and disableValidation() instead of these methods.

If you need a array of all the fields in a Dynaform, you can use:
getFormById("form-id").items.asArray();

For example:

Code: Select all

var aFields = getFormById( $("form").prop("id") ).items.asArray();
for (var i = 0; i < aFields.length; i++ ) {
   aFields[i].validate();
}
Amos B. Batto ♦ ProcessMaker Technical Documentation Writer & Forum Manager ♦ Cofounder of ILLA (http://www.illa-a.org)

amosbatto
Posts: 6143
Joined: Mon Jun 22, 2009 10:28 am
Country: Bolivia
Location: La Paz
Contact:

Re: Validate form from javascript

Unread postby amosbatto » Mon Apr 16, 2018 12:35 pm

allanrp1 wrote:So it's not possible...
What can i do? i want to execute a script after a web entry, is there some way to automatically pass to another task without a user review?

When the user clicks on a submit button, it should automatically do the validation and won't allow the user to submit without all the fields passing validation. If you don't want validation of the fields, then disable the fields that require validation with disableValidation() or create a separate form for web entry which doesn't have fields that require validation.
Amos B. Batto ♦ ProcessMaker Technical Documentation Writer & Forum Manager ♦ Cofounder of ILLA (http://www.illa-a.org)


Return to “Using ProcessMaker 3”

Who is online

Users browsing this forum: No registered users and 3 guests