0

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' => 'hidden',
    '#value' => 'INTERACTIVE',
);

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

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

$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->setValue('signature', $signature);

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

In validateForm()

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

And in buildForm() :

$signature = $form_state->getValue('signature');

$form['signature'] = array(
    '#type' => 'hidden',
    '#value' => $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.

  • 1
    Why don't you tell us what your form is supposed to do, as well as how you are trying to do it. We can then tell you headed the right way to go about achieving that goal, rather than trying to figure out what is wrong with your code. – Jaypan Jul 8 at 23:55
  • @Jaypan I've completely rewritten the question. Is it easier to understand ? – Sébastien Gicquel Jul 9 at 7:48
0

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->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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.