update_form_codeAdmin-only MCP tool.
Write one of the four procedures that Formulize runs at moments in the life of an entry in a form. To write the shared code library that belongs to an application rather than to a form, use update_application_code instead.
The code you send replaces that procedure completely. It is not added to what is there. Call get_custom_code first, and if you are adding to existing logic, include the existing code in what you send. Sending an empty string removes the procedure altogether.
Your code is placed inside a function that Formulize generates, so write the statements only - do not write a function declaration.
In the three save and delete procedures, the entry’s values arrive as variables named after the element handles: the value to be saved in an element with the handle ‘artifacts_year’ is available as $artifacts_year. This does not apply to form_custom_edit_check, which is about permission rather than about data and receives no element values.
The $currentValues array is how you tell what changed. In the save procedures the values that were in the database before this save operation started, are available as $currentValues[‘handle_name’]. For example, this lets you compare $currentValues[‘artifacts_year’] with $artifacts_year to see whether what’s in the database now is different from what is being/has been saved.
The values of the element handle variables, and of the $currentValues array, are all formatted for storage in the database. For example, a linked element’s value will be the entry_id of the selected entry or entries, not the human readable value the user selected. This is the opposite of how the other tools work. create_entries and update_entries accept readable values and convert them for you, and get_entries_from_form lets you filter on readable values too. Custom code has no such conversion in either direction: you read the stored values and you write the stored values, so you have to know how the data is actually held. For complete details of the functions and formulizeDataHandler methods available to this code, use the get_documentation tool, if it is available.
What each procedure receives, and what it should do:
There is no syntax checking when you save. A mistake will not be reported here - it will surface when someone next uses the form, so re-read what you wrote before finishing.
| Property | Type | Required? | Description |
|---|---|---|---|
| code_type | string (one of: form_on_before_save, form_on_after_save, form_on_delete, form_custom_edit_check) | Required | Required. Which of the form’s four procedures to write. |
| form_id | integer | Required | Required. The id of the form the procedure belongs to. |
| code | string | Required | Required. The PHP statements, with no function declaration. Send an empty string to remove the procedure. |