Skip to main content

Validations and saving data

Client-Side Validations

Client-side validations are performed immediately in the browser, providing instant feedback to users without requiring server communication. These validations are defined using JavaScript functions. Note that client-side validations are not a replacement for server-side validations. They are meant to enhance the user experience by providing immediate feedback.

How to Implement Client-Side Validations:

  1. Get a reference to your grid instance:
let ucGrid = $("#ucGrid_YOUR_GRID_ID").ucGrid("instance");
  1. Create a validation function for a specific column:
function validateColumn(newValue, row) {
// Validation logic here
if (!validationPasses) {
return "Your error message here.";
}
}
  1. Assign the validation function to the column in your grid configuration:
ucGrid.setValidationFunction("COLUMN_NAME", validateColumn);

Example

Refer to the sample app for an example.

Server-Side Validations

Server-side validations are performed on the server and thus require a round trip to the server. These validations are defined using PL/SQL functions or procedures. Server-side validations are essential for ensuring data integrity and security, as they cannot be bypassed by users.

Single Row Validations

Standard server-side validations operate on individual rows and are defined in a custom database procedure.

How to Implement Server-Side Single Row Validations:

  1. define a PL/SQL procedure that takes a UC_GRID_CRUD_REQUEST_T parameter.
PROCEDURE my_validation_proc(p_crud_request IN OUT NOCOPY UC_GRID_CRUD_REQUEST_T)
AS
-- Declare variables to hold column values
l_id NUMBER;
l_start_date DATE;
l_end_date DATE;
-- Additional variables as needed
BEGIN
-- Extract values from the request
l_id := p_crud_request.get_value('ID');

-- Perform validations
IF validation_fails THEN
-- Add an exception to the request
p_crud_request.add_exception(
p_column_name => 'COLUMN_NAME',
p_exception_text => 'Your error message'
);
END IF;
END my_validation_proc;
  1. Put the name / package.prc_name into the Validation Procedure attribute of the grid.

  2. Assure Validation Type = Row-by-row in the grid attributes.

Example

Refer to the sample app for an example.

All Rows Validations

For validations that require checking across multiple rows (such as sum calculations), use the "all rows" validation type.

How to Implement Server-Side All Row Validations:

  1. define a PL/SQL procedure that takes a UC_GRID_CRUD_REQUEST_TT parameter (array of requests).
PROCEDURE validate_all_rows(p_crud_requests IN OUT NOCOPY UC_GRID_CRUD_REQUEST_TT)
AS
l_crud_request UC_GRID_CRUD_REQUEST_T;
-- Additional variables as needed
BEGIN
-- Loop through all rows
FOR i IN p_crud_requests.first .. p_crud_requests.last LOOP
l_crud_request := p_crud_requests(i);
-- Extract and process values
END LOOP;

-- Add exceptions if validation fails
IF validation_fails THEN
FOR i IN p_crud_requests.first .. p_crud_requests.last LOOP
-- Only add exceptions to changed rows
IF p_crud_requests(i).crud_request IS NOT NULL THEN
p_crud_requests(i).add_exception(
p_column_name => 'COLUMN_NAME',
p_exception_text => 'Your error message'
);
END IF;
END LOOP;
END IF;
END validate_all_rows;
  1. Put the name / package.prc_name into the Validation Procedure attribute of the grid.

  2. Set Validation Type to All rows in the grid attributes.

  3. Consider enabling Load all rows and Process all rows to ensure the validation procedure sees all rows. Load all rows will disable pagination and load all rows at once. Process all rows will send all rows to the server, even if they are unchanged.

Example

Refer to the sample app for an example.

Data saving

After the server-side validations are passed, the data is saved to the database.

DML against a table/view

Enhanced Grid pro can automatically perform the DML (insert, update, delete statements) against a given table or view. Just input the object name into the "Table/View used for DML operations" attribute of the grid.

For views where no automatic DML is possible you can define "instead of triggers".

DML using a procedure

You can define custom DML operations using a PL/SQL procedure:

PROCEDURE my_save_proc(p_crud_request IN OUT NOCOPY UC_GRID_CRUD_REQUEST_T)
AS
-- Declare variables for column values
l_id NUMBER;
-- Additional columns as needed
BEGIN
-- Extract values from the request
l_id := p_crud_request.get_value('ID');
-- Extract other columns

-- Process based on operation type
CASE p_crud_request.crud_request
WHEN 'DELETE' THEN
DELETE FROM your_table WHERE id = l_id;

WHEN 'UPDATE' THEN
UPDATE your_table
SET column1 = value1,
column2 = value2
WHERE id = l_id;

WHEN 'INSERT' THEN
INSERT INTO your_table (column1, column2)
VALUES (value1, value2)
RETURNING id INTO l_id;

-- Set the generated ID back to the request
p_crud_request.set_value('ID', l_id);
END CASE;
END my_save_proc;

Refer to the sample app for an example.