On Validate

Tags:  

Form Actions - Validate


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 actions -> on add block within the Submit button is executed when a new form data is submitted. This script is executed before the new user data is stored in the database.

actions
{
on add
{

Submit
(
type = submit
displayname = "Submit"
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 actions -> on edit block within the Update button is executed when an existing record is modified and submitted. This script is executed before the modified user data is stored in the database.

actions
{
on edit
{
Update
(
type = submit
displayname = "Update"
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 "<Specify alert message>";
cancel submit;
}
}

)
}
}


Validate Form Action - On Delete : The validate form action script in the actions -> 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.

 actions
{
on delete
{
on validate
{
// write validate form action to be executed when an existing record is deleted
...............
{
// specify alert message if not valid and cancel submission
alert "<Specify alert message>";
cancel delete;
}
}

}

Examples


    To validate if a field value falls within a specified range

    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
    {
    Submit
    (
    type = submit
    displayname = "Submit"
    on validate
    {
    if (input.DateOfBirth.getYear() > zoho.currentdate.getYear())
    {
    alert "Enter valid Date of Birth";
    cancel submit;
    }
    }

    )
    }

    on edit
    {
    Update
    (
    type = submit
    displayname = "Update"
    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
    {
    Submit
    (
    type = submit
    displayname = "Submit"
    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

Related Links

Alert
Cancel Submit
Tips & Tricks - Validating Form Data
Tips & Tricks - Deleting child records (using on delete -> validate script)


0 Comments  Show recent to old
Post a comment


 RSS of this page