get_js_injection()
public function get_js_injection(): string
Description
Returns JavaScript code that injects validation errors into the page for client‑side highlighting. Includes two parts:
- A
<script>tag that setswindow.trongateValidationErrorsto a JSON representation of the errors. - A
<script src="...">tag that loads the framework's built‑in error‑highlighting JavaScript.
This method enables automatic field highlighting without additional developer JavaScript.
Parameters
This method accepts no parameters.
Return Value
| Type | Description |
|---|---|
| string | HTML script tags (or empty string if no validation errors exist). |
Example Usage
PHP
// Automatic injection in view template
<?php
if (isset($validation_errors)) {
echo $this->validation->get_js_injection();
}
?>
// Manual injection after failed validation
public function submit() {
$this->validation->set_rules('email', 'Email', 'required|valid_email');
if (!$this->validation->run()) {
$data['js_injection'] = $this->validation->get_js_injection();
$this->view('form', $data);
return;
}
}
// The injected JavaScript looks like:
// <script>window.trongateValidationErrors = {"email":["The Email field is required."]};</script>
// <script src="http://localhost/myapp/validation_module/js/highlight_validation_errors.js"></script>Notes
- Returns an empty string if
$_SESSION['form_submission_errors']is not set. - The JavaScript file
highlight_validation_errors.jsis part of the Validation module's assets. - JSON is sanitized with
JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOTflags for safe embedding. - The highlighting script automatically finds fields with errors and adds CSS classes (typically
validation-error). - For custom highlighting, you can read
window.trongateValidationErrorsin your own JavaScript.