How can I set value to hidden field (Form Api) and send them with post method? I’ve created a form with Form Api in a custom module.

I send datas with post method to a web service.

In buildForm()

$form['mode'] = array(
    '#type' =&gt; 'hidden',
    '#value' =&gt; 'INTERACTIVE',
);

$form['amount'] = array(
    '#type' =&gt; 'textfield'
);

$form['signature'] = array(
    '#type' =&gt; 'hidden',
     // here is the field I need to set its value on submit because its value depends on other field.
    '#value' =&gt; ''
 );

$form['#action'] = 'https://url-webservices.fr/‘


It works. But I need to set value to hidden fields 'signature' on submit.

How can I set value to hidden field and send them with form with post method ?

Something like this but it doesn't work :

in submitForm()

form_state-&gt;setValue('signature', $signature);


I’ve studied some examples and I can do that :

In validateForm()

$signature = 'string-to-send';
$form_state-&gt;setValue('signature', $signature);
$form_state-&gt;setRebuild();


And in buildForm() :

$signature = $form_state-&gt;getValue('signature');

$form['signature'] = array(
    '#type' =&gt; 'hidden',
    '#value' =&gt; $signature
);


Of course, it works only when page is reloaded when the form is rebuilt. If datas is send to an external URL with POST method, the field value is empty.
To summarize the question : I use an API where a SHA signature had to be sent along the form . That signature is calculated based on the other form inputs, so it had to be calculated after the user clicks the submit button and it had to be sent as a form field with POST method .

In buildForm(), I have :

$form['#action'] = 'https://url-webservices.fr/';


It is like it is executed before I can call validateForm() where I can set the value I need.

I've searched a lot for a solution but didn't find an easy way like set a news value in submitForm(). 

So here is a solution. I'm not completely happy with it but it works.

I've built a 2 steps forms.

In first step, the $form['#action'] is empty. After validation, I set a value to $form['#action'] and to the form fields I need :

$form_state-&gt;setValue('action', $url_action);


I also change the text of the submit button with something like "confirm".

Then user could confirm and validate the form.

All the mechanic of the 2 steps could be seen in that answer (thanx to Clive) :

How to use ConfirmFormBase to confirm the action of another form?