Documentation

On this page

Download full book (text)

# **Chapter 17: Built-in Form and Data Components — Select, TextArea, FileUploader, AJAX Editing** > **Prerequisites:** Chapters 5-7 (FrontFiles, HTMLForm, Pagination), Chapter 13 (AJAX) > **Try it:** Tested code from gate `ex17comps` > (`apps/examples/Ex17comps.gate.php` + `apps/examples/fronts/ex17comps_*.front`). > Open `http://localhost/SartajPhpDoc/ex17comps-install.html` once, then > `http://localhost/SartajPhpDoc/ex17comps.html`. --- ## **17.1 The Fusion Rule Again (It Matters Most Here)** Chapter 6 introduced `fui-*` (init once, at FrontFile build) vs `fun-*` (anytime, one owner). Component-heavy pages are where teams break this rule, so the chapter opens with it: - `fui-setForm`, `fui-setFileTypesAllowed`, `fui-setFileSavePath` → **Front only**, run during `new FrontFile(...)` in parent→child order - `fu_setPerPageRows`, `fu_setWhere` → **Gate only** when a Page Event may change them (search, filters) - Never both for the same setting The FileUploader makes this concrete: its save path and MIME allow-list are `fui-*` because the upload is processed during FrontFile construction (`oncreate → saveFile`) — **before** your Page Event ever runs. ## **17.2 Select with Options from the Front** ```html <select id="sltcategory" runat="server" class="form-select" dfield="sltcategory" dtable="ex17_doc" fui-setForm="form2" fun-setMsgName="Category" fun-setOptions="general=General,policy=Policy,manual=Manual" fun-setFirstValue="— pick —"></select> ``` | Attribute | Effect | |-----------|--------| | `fun-setOptions="val=Label,val=Label"` | Static option list | | `fun-setFirstValue="— pick —"` | Extra empty first option | | `fun-setMsgName="Category"` | Name used in validation messages | | `dfield` + `dtable` + `fui-setForm` | DB binding (Chapter 4) | For dynamic options, fill from the Gate before render: `$slt->fu_setOptions($assocArray)` or loop `addOption($value, $label)` — Gate-owned, so keep it out of the Front. ## **17.3 TextArea** ```html <textarea id="txabody" runat="server" class="form-control" rows="6" dfield="txabody" dtable="ex17_doc" fui-setForm="form2" fun-setMsgName="Body" placeholder="HTML or text content stored in DB"></textarea> ``` Nothing exotic — but note the DB stores the text; escape on **output** (`htmlspecialchars`) unless you intentionally store HTML. ## **17.4 FileUploader — `type="file"` + `fui-*` Config** Any `runat="server"` input with `type="file"` becomes a FileUploader. The tested edit form (`ex17comps_edit.front`): ```html <form id="form2" runat="server" method="post" enctype="multipart/form-data" action="##{getThisGateUrl()}#" class="mb-3"> <input id="txttitle" runat="server" type="text" class="form-control" dfield="txttitle" dtable="ex17_doc" fui-setForm="form2" fun-setMsgName="Title" fun-setRequired="" /> <!-- type=file → FileUploader. fui-* at init before oncreate/saveFile. --> <input id="file1" runat="server" type="file" dfield="txtfilepath" dtable="ex17_doc" fui-setForm="form2" fun-setMsgName="Attachment" fun-setDisplayFile="" fui-setFileTypesAllowed="image/jpeg,image/png,image/gif,application/pdf" fui-setFileMaxLen="2097152" fui-setFileSavePath="##{$parentGate->uploadDir . $file1->generateFileName()}#" /> <button type="submit" class="btn btn-primary">##{$parentGate->formButton}#</button> </form> ``` Key points (all tested): 1. **`enctype="multipart/form-data"`** on the form — uploads silently fail without it 2. **`fui-setFileSavePath`** composes the final path: `$parentGate->uploadDir` (Gate property, **trailing slash required** — Front concatenates `uploadDir . generateFileName()`) 3. **`fui-setFileTypesAllowed`** is a MIME allow-list — blocks PHP uploads even if the extension is renamed 4. **`fui-setFileMaxLen`** caps bytes (2097152 = 2 MB) 5. **The DB stores the path, not the bytes** — `dfield="txtfilepath"` binds the generated filename into your table column 6. **Do not call `fi_*`/`fui_*` after `new FrontFile`** for upload settings — `saveFile` already ran during construction. If you must change the path logic, do it before the FrontFile is created (Gate property). Gate side keeps only business flow (tested): ```php <?php // apps/examples/Ex17comps.gate.php /** Trailing slash required — Front concatenates uploadDir . generateFileName() */ public $uploadDir = PROJ_PATH . '/uploads/ex17/'; public function onstart() { // ... // Ensure upload dir exists (filesystem only — FileUploader settings are on the Front) if (!is_dir($this->uploadDir)) { @mkdir($this->uploadDir, 0777, true); } // FrontFile() builds DOM + runs fi_* (fui-*) in parent→child order, // including FileUploader oncreate → saveFile. Do not call fi_* after this. $this->genFormFront = new FrontFile('edit'); $this->showallFront = new FrontFile('list'); $this->setMasterFile($masterf); // fu_* only in Gate (not also on Front) — Page Events can override later $showall = $this->showallFront->getComponent('showall'); $showall->fu_setPerPageRows(8); $showall->fu_setWhere(' ORDER BY id DESC '); } ``` ## **17.5 AJAX Row-Click Editing (addJsonFront Pattern)** Click a list row → the edit Front (filled by `viewData`) swaps into a holder — no page change (Chapter 13's `addJsonFront` doing real CRUD work): ```php <?php /** AJAX: row click → fill edit Front into #edtHolder */ public function page_event_rowclick($evtp) { $form2 = $this->genFormFront->getComponent('form2'); $this->page->viewData($form2, $evtp); $this->pageName = 'Edit #' . $evtp; $this->formButton = 'Update'; $this->jsServer->addJsonFront($this->genFormFront, 'edtHolder'); } ``` List row triggers it: ```html <tr style="cursor:pointer" onclick="sendAjax('##{getEventUrl('rowclick', $showall->getRow('id'))}#');"> ``` AJAX pagination of the same list (Chapter 13's `addJsonComp` + `addJsonBlock`): ```php <?php /** Pagination page change (AJAX) */ public function page_event_showall_show($evtp) { $showall = $this->showallFront->getComponent('showall'); $this->jsServer->addJsonComp($showall, 'showall'); $this->jsServer->addJsonBlock('html', 'pagebar', $showall->getPageBar()); } ``` ## **17.6 Validation Flow Around Uploads** `insertData` runs only when validation passed — check between steps (tested): ```php <?php private function doInsert($form2) { global $cmpId; $extra = array(); $extra[]['userid'] = 0; $extra[]['parentid'] = 0; $extra[]['submit_timestamp'] = time(); $extra[]['update_timestamp'] = time(); $extra[]['spcmpId'] = $cmpId; if (!getCheckErr()) { // fusion validation (required, MIME, size) $this->page->insertData($form2, $extra); } if (!getCheckErr()) { setMsg('ex17', 'Inserted (filepath column stores upload path only)'); $this->setFrontFile($this->showallFront); } else { $this->setFrontFile($this->genFormFront); // stay on form, errors show } } ``` `fun-setRequired=""` + `fun-setMsgName="Title"` produce the user-facing error; `getCheckErr()` is the Gate's single "is everything OK" probe. ## **17.6a `fur-_` — The Generic Attribute Setter** Every fusion method maps to a `fu_*`/`fi_*` method — except one. The special `_` fusion sets **any HTML attribute** on the Component's tag directly, with expression-tag values evaluated at render time (tested on `ex06comp.html`): ```html <p id="pFurProbe" runat="server" fur-_style="color: ##{$parentGate->probeColor}#; font-weight:bold;">...</p> ``` Rendered output (verified): `<p id="pFurProbe" style="color: red; font-weight:bold;">` — the Gate property `probeColor` flowed straight into the `style` attribute with no Gate-side `setAttribute()` call. Use it for `class`, `style`, and `data-*` attributes: ```html <div id="div1" runat="server" fur-_class="badge ##{$parentGate->isOk ? 'bg-success' : 'bg-danger'}#" fur-_data-recid="##{$parentGate->recId}#"></div> ``` This is the declarative twin of `$comp->setAttribute('style', ...)` in the Gate — same effect, but it survives AJAX re-renders because it re-evaluates on every render (that's why it's `fur-`, render phase). Rule of thumb: static attributes go in plain HTML, permission logic uses Chapter 14's fusion methods, and *dynamic* plain attributes use `fur-_`. ## **17.7 Component Quick Reference (this chapter)** | Component | Front trigger | Key attributes | |-----------|--------------|----------------| | Text input | `type="text" runat="server"` | `dfield`, `dtable`, `fui-setForm`, `fun-setRequired` | | Select | `<select runat="server">` | `fun-setOptions`, `fun-setFirstValue` | | TextArea | `<textarea runat="server">` | `rows`, `dfield` binding | | FileUploader | `type="file" runat="server"` | `fui-setFileTypesAllowed`, `fui-setFileMaxLen`, `fui-setFileSavePath` | | Pagination | `path="uikit/data/Pagination.php"` | `fun-setFieldNames`, `dtable`, `fun-setPerPageRows` | | HTMLForm | `<form id runat="server">` | `fui-*` init, `isUpdateRequest()` in Gate | ## **17.8 Verify It Yourself** Against `http://localhost/SartajPhpDoc/`: 1. `ex17comps-install.html` → schema (`ex17_doc`) 2. `ex17comps-addnew.html` → pick a PNG/JPEG/PDF under 2 MB → save → row in list 3. Try uploading a `.txt` → rejected by MIME allow-list (soft error on form) 4. Click a list row → edit form appears in the holder (AJAX `addJsonFront`) 5. Update → "Updated" 6. Check `uploads/ex17/` — the file is on disk; the DB column holds only the path --- **Previous:** [Chapter 16 — Data Log History and Recycle Bin](chapter16.md) · **Next:** [Chapter 18 — AutoGate: CRUD With Less Code](chapter18.md)