Simple actions with form

GET for fetching data based on query criteria

Note

Do not use any form control inputs while submitting the form using GET method (at least for now there is no practical use of form control options in GET requests).

Use GET requests to fetch multiple data for tables using Query model, or to fetch single Input entity (usually just passing its ID) while defining link to the edit form.

Query models always connected with the List models. When defining the List model in your template, GOERP will make request to the related API and fetch data based on the Query parameters.

<form method="get" id="query-form">
  <label for="query-parameter-1">Query parameter 1:</label>
  <input type="text" id="query-parameter-1" name="Api.ModelQuery.Field"
         value="{{ .Data.Api.ModelQuery.Field }}">

  <label for="query-parameter-2">Query parameter 2:</label>
  <input type="text" id="query-parameter-2" name="Api.ModelQuery.AnotherField"
         value="{{ .Data.Api.ModelQuery.AnotherField }}">

  <button type="submit">Find</button>
</form>

<!-- Use List entities to actually apply the query, otherwise Query parameters will be ignored -->
<ul>
  {{ range .Data.Api.ModelList }}
  <li>{{ .Name }}</li>
  {{ end }}
</ul>

POST for create and update actions

Let’s take a look into simple create/update example form:

<form method="post" id="post-form">
  <!-- For saving action need to pass only postActionEntity, which will ensure that you are working with correct model -->
  <input type="hidden" name="postActionEntity" value="ModelInput">

  <!-- Optionally, use redirect option to redirect to the specified page after successful saving -->
  <input type="hidden" name="postActionRedirect" value="https://erply.com/">

  <!-- Next 2 fields are related to actual model parameters -->
  <label for="input-1">Input 1:</label>
  <input type="text" id="input-1" name="Api.ModelInput.Field"
         value="{{ .Data.Api.ModelInput.Field }}">

  <label for="input-2">Input 2:</label>
  <input type="text" id="input-2" name="Api.ModelInput.AnotherField"
         value="{{ .Data.Api.ModelInput.AnotherField }}">

  <button type="submit" id="save">Save</button>
</form> 

Often we need to make reference from the list of rows to the specific entity edit form. In this case pass entity ID from the selected row to the edit link by attaching it to the Input ID (some input models doesn’t have Input suffix, but you can check in data source definitions in editor if model have “write” option):

<!-- Use built-in .Session object to access account related configuration -->
<!-- Use  prefix '$' if linking made inside the range loop, otherwise omit it -->
<a href="/{{ $.Session.ClientCode }}/{{ $.Session.Language.Code }}/example-page?Api.ModelInput.Id={{ .Id }}"
   target="_self" class="table-action-button">
  <i class="button nowrap  button--transparent icon-Edit-Line"></i>
</a>

POST for delete action

Delete actions should be made by following next steps:

  • Form must have method parameter set to POST
  • Inside the form set hidden postAction input to delete
  • Inside the form set hidden postActionIds input to entity IDs (separated by comma) that should be deleted (using custom JS or predefined goerp components)
  • Inside the form set hidden postActionEntity input to the entity name that should be deleted. If it is row in the table, then that entity should have suffix List (e.g. WarehouseDtoList or WarehouseDtoList). If deleting from the single entity edit form, then suffix may be Input or just model name, check data sources definition in the editor for exact model names. Please note, postActionEntity value shouldn’t have API definition, just model name.
<form method="get" id="entity-table-id">
  <input type="hidden" name="postAction" value="delete">
  <input type="hidden" name="postActionIds" value="">
  <input type="hidden" name="postActionEntity" value="ModelList">
  <!-- Add submit button if delete modal not used -->
  <!-- <button type="submit">DELETE</button>-->
</form>

<!-- and then somewhere in the list range -->
<ul>
  {{ range .Data.Api.ModelList }}
  <li>{{ .Name }}</li>
  <!-- If used delete-confirm-partial component, then define next parameters inside the button -->
  <button data-form-id="entity-table-id"
          data-delete-identifier="{{ .Id }}"
          data-post-action-entity="ModelList"
          class="button button--transparent  nowrap table-action-button action-row-red form-delete-button">
    <i class="icon-Trash-Empty-NotFilled"></i>
  </button>
  {{ end }}
</ul>

<!-- As an option, somewhere in the end of the page import delete-confirm-modal from goerp components and use it -->
<!-- to delete one or many entities. Or write your own deletion logic -->
{{ template "delete-confirm-partial" .}}