Homepage

Default Payload

Last edit: Jul 14, 2023

The default_payload option in Forms allows you to provide default request parameters as JSON. Provided data and request parameters are joined together
before validation. All attributes passed this way must be defined in the Form.

Why and where to use default_payload

Default Payload can be used in all forms that need to include data not directly sent by a user in a form submission. Request data is processed in Liquid and then merged with request data as new or original attribute. A good example can be a slug property that is formatted based on title/name property.

Another important feature is that default_payload allows you to overwrite/extend user input on the server side. You can use it, for example, to store references, calculate prices, etc., because users have no way to maliciously change the value, even if they try to edit a form's HTML.

Form variable

Liquid Template provides you with a form variable that is populated with various properties useful when dealing with forms.

Example

Default Payload can help with setting action timestamp, states, or modification of request state.
In the example below the custom property cancelled_at is set with each form submission:


---
...
fields:
  properties:
    cancelled_at:
      validation:
        presence: true
default_payload: |-
  {%- assign date_now = 'now' | date: '%d-%m-%Y %H:%M:%S' -%}
  {
    "properties_attributes": {
      "cancelled_at": "{{ date_now }}"
    }
  }
---

Record Example


---
...
fields:
  properties:
    some_custom_property:
      validation:
        presence: true
default_payload: |-
  {
    "properties_attributes": {
      "some_custom_property": "New value for this property"
    }
  }
---

Questions?

We are always happy to help with any questions you may have.

contact us