path: '/payment/{submission}'
    _form: '\Drupal\mymodule\Form\PaymentForm'
    _title: 'Pay Fee'
    _role: 'applicant'
        type: entity:webform_submission

This works:

public function buildForm( array $form, FormStateInterface $form_state, WebformSubmission $submission = NULL) {
    $submission_data = $submission->getData();

This doesn't (null):

public function submitForm(array &$form, FormStateInterface $form_state, WebformSubmission $submission = NULL) {
    $submission_data = $submission->getData();

Is it intentional that upcasting is only available to buildForm?
Is there a workaround?

(My logic being: the form, when submitted, still POSTs to /payment/123, so it's still convenient to load the object.)

  • 2
    I think the form is still built when it's submitted, you could try just stashing the object in the buildForm method: $this->submissionData = $submission_data, them picking it up again on submit. If not, just store it on the form ($form['#submission_data'] = $submission_data), and pick it up that way. Or store the ID if you'd rather ensure you get a fresh submission object considering the form can be cached – Clive Aug 2 '18 at 14:03
  • Thank you! Storing in the object works, as does using '#type' => 'value' and $form_state->getValues() to retrieve it. (Assuming former is more efficient.) – William Turrell Aug 2 '18 at 14:35

Yes, this is intentational. submit and validate callbacks aren't tied to the request, they only receive exactly those two arguments.

As mentioned in the comments, you can store the information either on the object, with $form_state->get()/set() or a form elemnt of #type value, doesn't make a big difference which you chose.

You could also store only the ID instead of the whole object but actually, I believe it is stored anyway in form_state somewhere when you pass it as an argument to the form (pretty well hidden though, so better to store & access it in a more explicit place)

| improve this answer | |

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.