# Add Validation Logic

ONEWEB Smart Forms support **custom validation logic** to ensure data accuracy and enforce business rules before information is processed or saved.

There are **two supported approaches** for adding validation logic:

1. **JavaScript Validation** – client‑side validation
2. **Java Validation** – server‑side validation

Each approach serves a different purpose and can be used independently or together, depending on application requirements.

***

### JavaScript Validation (Client‑Side)

**JavaScript validation** is primarily used for **client‑side checks**, such as:

* Mandatory field validation
* Input format validation
* Immediate user feedback before form submission

JavaScript validation improves usability by preventing invalid data entry at an early stage.

***

#### Attach JavaScript via Entity Action

ONEWEB Smart Forms use **Entity Actions** to attach a JavaScript file to an entity.

> Refer to **Entity Actions** documentation for detailed configuration: <https://docs.oneweb.tech/oneweb/design-and-develop-oneweb-apps/design-and-develop-ux-ui/ux-ui-components/smart-forms/buttons-and-actions>

1. Add a **Module Action** and specify the JavaScript file name.

<figure><img src="https://2015371994-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FMpDjHWFRUtZ5nJcSfVXd%2Fuploads%2FzB2xGAKTwN9hXallLhRs%2F0?alt=media" alt=""><figcaption></figcaption></figure>

***

#### Create JavaScript Validation Logic

1. Open the ONEWEB workspace in **Eclipse IDE**.
2. Create the JavaScript file specified in the Entity Action.
3. Add the validation function inside the file.

<figure><img src="https://2015371994-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FMpDjHWFRUtZ5nJcSfVXd%2Fuploads%2FkotOnVaj43q7df43QrmL%2F1?alt=media" alt=""><figcaption></figcaption></figure>

***

#### Bind Validation to Form Field

1. Build and publish the project to the server.
2. In **App Designer**, edit the target form field.
3. Open the field configuration (pencil icon).
4. Add the JavaScript validation function call in the **Source Tag** property.

<figure><img src="https://2015371994-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FMpDjHWFRUtZ5nJcSfVXd%2Fuploads%2FwvAqwO1AYbqC4U3Ba2gO%2F2?alt=media" alt=""><figcaption></figcaption></figure>

The validation logic is now executed on the client side when the user interacts with the field.

***

### Java Validation (Server‑Side)

**Java validation** is used for **server‑side validation** and is executed **before saving the entity**.\
This approach is critical for enforcing business rules that must not be bypassed, even if client‑side validation is skipped.

***

#### Attach Java Class via Entity Action

1. Add an **Entity Action** to the entity.
2. Specify the Java class name in the **Class Action** property.

<figure><img src="https://2015371994-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FMpDjHWFRUtZ5nJcSfVXd%2Fuploads%2FpY0sDH2DSP9wyg2Y0Tk0%2F3?alt=media" alt=""><figcaption></figcaption></figure>

***

#### Implement Validation Class

1. Open the ONEWEB workspace in **Eclipse IDE**.
2. Create the Java class with the following structure:

* Extend **`ProcessHelper`**
* Implement **`ProcessAction`**

3. Override the `validateResult()` method to define custom validation logic.

**Example (Server‑Side Validation)**

```
public class OrderRequestBackOfficeUpdateMode
        extends ProcessHelper
        implements ProcessAction {

    @Override
    public boolean validateResult() {

        String entityID =
            (String) getRequest()
            .getSession()
            .getAttribute("entityID");

        EntityFormHandler entityForm =
            (EntityFormHandler) getRequest()
            .getSession()
            .getAttribute(entityID + "_session");

        Vector errorVect = entityForm.getFormErrors();

        try {
            int incompleteCount =
                ManualDAOFactory
                .getManualDocOrderDAO()
                .countInCompleteOrderUnit("REQUEST_ID");

            if (incompleteCount > 0) {
                errorVect.add(
                    "Please verify unassigned order units."
                );
                return false;
            }

        } catch (Exception e) {
            errorVect.add(e.getMessage());
            return false;
        }

        return super.validateResult();
    }
}
```

> For more advanced examples, refer to the Sample Apps validation documentation: <https://docs.oneweb.tech/oneweb/sample-apps/order-management-app-web/app-designer-customize/validate-order-details-when-back-office-submits>

***

#### Deploy Java Validation

1. Build and publish the project to the server.

From this point forward, ONEWEB will **automatically execute the Java validation** every time before the entity is saved.

***

### Summary

ONEWEB Smart Forms provide robust validation capabilities through both **client‑side** and **server‑side** mechanisms.

| Validation Type | Purpose                                              |
| --------------- | ---------------------------------------------------- |
| JavaScript      | Faster feedback, input format checks, UX improvement |
| Java            | Enforced business rules, data integrity, security    |

Using both approaches together ensures:

* Better user experience
* Strong data integrity
* Protection against invalid submissions
* Enterprise‑grade validation behavior

This layered validation model allows ONEWEB applications to scale safely while maintaining usability and correctness.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.onewebstack.com/building-apps/ui-page-designer/ux-ui-customization/smart-form-customization/add-validation-logic.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
