| Posted by: WUICTeam - Sunday, November 8, 2026 9:01:04 AM |
|---|
| [b]Update:[/b] a custom validation script that does not compile now fails the rule and shows the error, instead of letting every value through; the documentation explains the strict [c]true[/c] result and the empty-field case. Fixed in [url=https://wuic-framework.com/downloads/release-notes#v1.8.2]WUIC Framework 1.8.2[/url]. [i]The original answer below describes versions before 1.8.2.[/i] [b]Question[/b] I added a script to the custom validation callback of a column. The rule seems correct, but the form refuses to save, or the check behaves oddly when the field is empty. What does the callback have to return, and when does it run? [b]Answer[/b] The callback must [b]explicitly return the boolean[/b] [c]true[/c] when the value is valid. The framework tests the result with a strict comparison, so anything that is not exactly [c]true[/c] (no [c]return[/c] at all, a string, a number, a BehaviorSubject) counts as a failed validation. The script is the body of an async function with the arguments [c]record, field, vr, wtoolbox[/c]. [c]record[/c] holds BehaviorSubjects, so you read a value with [c].value[/c]. To show your own text, set [c]vr.message[/c] before returning [c]false[/c]. Two behaviours that are easy to miss: [list] [*]Unlike the built-in format and length rules, the custom rule also runs when the field is empty. That is what makes "required only if another field is filled" possible, but it also means your code must cope with [c]null[/c] and empty strings. [*]If the script throws, the validation fails and the user sees an error dialog. If the script cannot be compiled, the rule is silently replaced by one that always returns [c]true[/c], so check the browser console when a rule seems to do nothing. [/list] A working example (the field is mandatory when [c]colonna_numero[/c] has a value): [code] const v = record[field.mc_nome_colonna].value; if ((v === null || v === undefined || v === '') && record['colonna_numero'].value) { vr.message = 'Required field'; return false; } return true; [/code] A frequent mistake is returning the tested value itself, for example [c]return record.codice.value;[/c]. It is truthy but not [c]true[/c], so the save is blocked. Return a comparison such as [c]return !!record.codice.value;[/c] or an explicit [c]true[/c]/[c]false[/c]. [url=https://wuic-framework.com/docs/callback-cookbook]Documentation: Callback & metadata cookbook[/url] |