| | 69 | For every DB table, you can define functions to be invoked upon certain CRUD events. Those "callbacks" can be: |
| | 70 | |
| | 71 | - a single callable (function, lambda, callable object) |
| | 72 | - a list of callables, which are invoked in list order |
| | 73 | - a dict of callables, where the tablename is used as key to find the callable to be invoked |
| | 74 | - a dict of lists of callables, where the tablename is used as key to find the list of callables to be executed in list order |
| | 75 | |
| | 76 | The return value of the callables, if any, is ignored. |
| | 77 | |
| | 78 | ==== Validation Callbacks ==== |
| | 79 | |
| | 80 | After successful submission of a create/update form (=the input data have successfully passed the field validation), you can define extra form validation methods to be invoked by using: |
| | 81 | |
| | 82 | {{{ |
| | 83 | s3xrc.model.configure(table, create_onvalidation=callback) |
| | 84 | }}} |
| | 85 | {{{ |
| | 86 | s3xrc.model.configure(table, update_onvalidation=callback) |
| | 87 | }}} |
| | 88 | |
| | 89 | where: |
| | 90 | |
| | 91 | - '''table''' is the respective DB table |
| | 92 | - '''callable''' is the callback setting, see [#Callbacks Callbacks] |
| | 93 | |
| | 94 | If either of {{{create_onvalidation}}} or {{{update_onvalidation}}} is not set, then the {{{onvalidation}}} setting is tried: |
| | 95 | |
| | 96 | {{{ |
| | 97 | s3xrc.model.configure(table, onvalidation=callback) |
| | 98 | }}} |
| | 99 | |
| | 100 | This allows you to define a common onvalidation callback for both ''create'' and ''update''. |
| | 101 | |
| | 102 | Onvalidation callbacks are meant to allow form data validation, where any validation errors are to be put directly into the form as: |
| | 103 | |
| | 104 | {{{ |
| | 105 | form.errors[fieldname] = error_msg |
| | 106 | }}} |
| | 107 | |
| | 108 | where: |
| | 109 | |
| | 110 | - '''fieldname''' is the field containing the invalid value |
| | 111 | - '''error_msg''' is the error message to be displayed in the form close to that field |
| | 112 | |
| | 113 | The callables receive the form as first and only parameter, and their return values will be ignored. |
| | 114 | |
| | 115 | '''Important:''' |
| | 116 | On-validation callbacks are also invoked in non-interactive data imports, where multiple records are being processed in one and the same request. Therefore the callback '''must not redirect'''! |
| | 117 | |
| | 118 | ==== On-accept Callbacks ==== |
| | 119 | |
| | 120 | ==== On-Delete Callback ==== |