formtools / core

The Form Tools Core.
https://formtools.org
207 stars 78 forks source link

Submission pre-parser update hidden field #662

Open slfhstr opened 4 years ago

slfhstr commented 4 years ago

I have 3.0.20 with Submission Pre-Parser 2.0.5 I have a field defined in form but not editable called 'computed' as field and DB column So it doesn't show in $_POST but is in DB THIS (taken from other posts here) DOES NOT WORK ...

$db = FormTools\Core::$db; $db->query(" UPDATE ft_form_3 SET computed ='crocodile' WHERE submission_id = :submission_id "); $db->bindAll(array( "computed" => "crocodile", "submission_id" => $submission_id )); $db->execute();

BUT THIS WORKS (same but hard-coding submission id)....

$db = FormTools\Core::$db; $db->query(" UPDATE ft_form_3 SET computed ='crocodile' WHERE submission_id = 41 "); $db->bindAll(array( "computed" => "crocodile", "submission_id" => 41 )); $db->execute();

What's going on? How should current submission be referenced ? TVMIA

slfhstr commented 4 years ago

Also strikes me that maybe I should not be updating DB before the review page I should just be filling the data array But I'm not clear how to do that

slfhstr commented 4 years ago

I worked out a work-around to stay within the Submission Pre-Parser concept with minimal frigging about, and which is re-usable in different forms

1) add a field to the Form list of fields, e.g. "computed"

2) create a View Field Group in the Form View and give it a name like "hidden" (or "system" or "reserved" or whatever suits your approach and use cases - just be consistent in the following steps).

3) add the field "computed" to the "hidden" View Field Group

4) make the field editable ... then it will be available within the normal $_POST array

5) edit the Form Page of the template you are using to add these lines

     {{if $group.group_name == 'hidden'}}
      <div style="display:none">
       {{/if}}

before the line which says ...

    <h3>{{$group.group_name|upper}}</h3>

THEN add these lines ....

    {{if $group.group_name == 'hidden'}}
       </div>
        {{/if}}    

before the line which says ...

    {{if $fields|@count > 0}}

CLICK UPDATE TEMPLATE BUTTON

6) edit the Review Page of the template you are using to add these lines ...

    {{if $group.group_name == 'hidden'}}
      <div style="display:none">
       {{/if}}    

before the line which says ...

<h3><a href="?page={{$group.custom_data|default:1}}#s{{$group.group_id}}">EDIT</a>{{$group.group_name|upper}}</h3>

THEN add these lines ....

   {{if $group.group_name == 'hidden'}}
       </div>
        {{/if}}    

before the line which says ...

{{continue_block}}

CLICK UPDATE TEMPLATE BUTTON

7) Add your chosen code into the Submission Parser module (which allows different code per form), along with any logic you need, e.g.

      $_POST["computed"] = 'some value';

Now whenever you need, you can choose the relevant template, and add whatever computational fields you need to a form, make them editable but include them in a hidden View Field Group, and add logic to the Submission Pre-Parser module. The hidden fields will not show on the Form pages or the Review page, but will appear in the form submission.

Yes, it's a frig, but hopefully a re-useable one and one which respects the structure and intent of the module and template framework.

If there is an easier way, then please do share it !!