Form Actions - Validate



Description

 

An application that gives a free ride to input incorrect data is useless. By default, Zoho Creator supports validation for an adequate number of field types. You can specify the constraints for the fields you are defining while creating a form. For example, if you define a field to be of type Email, Zoho Creator checks if the data entered in the form for that field is a valid emailid, else it will display an error message to the user.

 

In addition to the default validation, Zoho creator allows you to write action scripts for custom validation which is called when a user submits the form to the server, before the user data is stored in the database. The script runs on the server side, and it has access to all the inputs the user entered. The syntax for the user input is input.<variable_name> as the examples below indicate.

 

The default action of validate is to submit the data, so you have to do a cancel submit if you want to stop a form from being submitted. You can also choose to provide proper alert message to the user while cancelling. The same form is then shown to the user to re-enter the data.

 

The Validate action scripts are executed when a new form data is submitted or when an existing record is modified and submitted or when an existing record is deleted.

 

Syntax

Validate form action - On Add : The validate form action script written in the On Add block is executed when a new form data is submitted. This script is executed before the new user data is stored in the database.

 

on  add
{
on validate
{
// write validate form action to be executed when a new form data is submitted
{
// specify alert message if not valid and cancel submission
alert "<Specify alert message>";
cancel submit;
}
}

}

 

Validate form action - On Edit : The validate form action script in the On Edit block is executed when an existing record is modified and submitted. This script is executed before the modified user data is stored in the database.

 

 

on  edit
{
on validate
{
// write validate form action to be executed when an existing record is modified and submitted
{
// specify alert message if not valid and cancel submission
alert "Enter valid Date of Birth";
cancel submit;
}
}

}

 

Validate form action - On Delete : The validate form action script in the On Delete block is executed when an existing record is submitted for deletion. This script is executed before the user data is deleted from the database.

 

 on  delete
{
on validate
{
if (input.Email_id != zoho.loginuserid)
{
alert "You can delete only your records";
cancel delete;
}
}
}

 

Examples - Free Flow Scripting

    To validate if a field value falls within a specified range - on add / on edit

     

    In the following sample, if the value specified in the age field is less than 20 or greater than 100, the submit action will get cancelled.

     

    on  add
    {
    on validate
    {
    if (input.DateOfBirth.getYear() > zoho.currentdate.getYear())
    {
    alert "Enter valid Date of Birth";
    cancel submit;
    }
    }

    } on edit
    {
    on validate
    {
    if (input.DateOfBirth.getYear() > zoho.currentdate.getYear())
    {
    alert "Enter valid Date of Birth";
    cancel submit;
    }
    }

    }

     

    To check for duplication of records - on add

     

    In the below example, if the same team member is added more than once, the submit action will get cancelled.

     

    on  add
    {
    on validate
    {
    if (count(team_member[name == input.name]) > 0)
    {
    alert "Name already exists";
    cancel submit;
    }
    }

    }

     

    where,

     

    team_member [name == input.name] - selects all the team_members whose name is equal to the name currently entered.
    input.name - it is the value for the field "name" given by the user while submitting the form

    count - count operator returns the number of team members whose name is equal to this.name

     

     

    To restrict registrations to your application - on add

    The following deluge code, checks for the required condition for cancelling submission. Here, the count function is used to count the number of records in a database. If the condition is satisfied, displays an alert message to the user and cancels the submission so that the record is not stored in database.


    on  add
    {
    on validate
    {
    if(count(Employee)>= 20)
    {
    alert("No more registrations allowed");
    cancel submit;
    }
    }

    }

    The following deluge code, uses the zoho.currentdate function to validate if the registration date has expired.

    on  add
    {
    on validate
    {
    if (zoho.currentdate > '20-Mar-2007')
    {
    alert "time for registration expired"; cancel submit;
    }
    }

    }

     

    To update the value of a field based on the values in other fields - on add/ on edit

     

    In the following deluge code, the value of the field PatientID is calculated based on the form values specified for the First_Name and Last_Name and the value returned by the variable zoho.currenttime.

     

    on  add
    {
    on validate
    {
    input.PatientID = input.First_Name + " " + input.Last_Name + " " + zoho.currenttime;
    }

    } on edit
    {
    on validate
    {
    input.PatientID = input.First_Name + " " + input.Last_Name + " " + zoho.currenttime;
    }

    }

     

     

    To restrict users to delete only thier own records - on delete

     

    In the following deluge code, the value of the field PatientID is calculated based on the form values specified for the First_Name and Last_Name and the value returned by the variable zoho.currenttime.

     

    on  delete
    {
    on validate
    {
    if (input.Email_id != zoho.loginuserid)
    {
    alert "You can delete only your records";
    cancel delete;
    }
    }
    }

 

 

Example - Using Script Builder

 

Refer the example in the topic, Statements -> Control Statements -> Cancel Submit.

 

Related Links:

 

FAQ's - Form Actions

Alert

Cancel Submit